From 6511920cffabee7ce9920578b41c83daf831e117 Mon Sep 17 00:00:00 2001 From: styu12 Date: Thu, 11 Aug 2022 21:38:01 +0900 Subject: [PATCH 1/3] solve 1260, 2178, 7562 --- graph2/1260/seungoh.py | 41 +++++++++++++++++++++++++++++++++++++++ graph2/1325/seungoh.py | 37 +++++++++++++++++++++++++++++++++++ graph2/17070/seungoh.py | 14 ++++++++++++++ graph2/2178/seungoh.py | 32 ++++++++++++++++++++++++++++++ graph2/7562/seungoh.py | 43 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 graph2/1260/seungoh.py create mode 100644 graph2/1325/seungoh.py create mode 100644 graph2/17070/seungoh.py create mode 100644 graph2/2178/seungoh.py create mode 100644 graph2/7562/seungoh.py diff --git a/graph2/1260/seungoh.py b/graph2/1260/seungoh.py new file mode 100644 index 0000000..7094af6 --- /dev/null +++ b/graph2/1260/seungoh.py @@ -0,0 +1,41 @@ +import sys +from collections import deque + +def dfs(graph, n, visited): + visited[n] = True + print(n, end=" ") + if len(graph[n]) == 0: + return + for i in graph[n]: + if not visited[i]: + dfs(graph, i, visited) + +def bfs(graph, start, visited): + queue = deque([start]) + visited[start] = True + print("") + print(start, end=" ") + while queue: + v = queue.popleft() + if len(graph[n]) == 0: + return + for i in graph[v]: + if not visited[i]: + queue.append(i) + visited[i] = True + print(i, end=" ") + print("") + +n, m, start = map(int, sys.stdin.readline().split()) +visited = [False] * (n+1) +graph = [[] for _ in range(n+1)] +for _ in range(m): + x, y = map(int, sys.stdin.readline().split()) + graph[x].append(y) + graph[y].append(x) +for i in range(len(graph)): + graph[i].sort() + +dfs(graph, start, visited) +visited = [False] * (n+1) +bfs(graph, start, visited) diff --git a/graph2/1325/seungoh.py b/graph2/1325/seungoh.py new file mode 100644 index 0000000..549708f --- /dev/null +++ b/graph2/1325/seungoh.py @@ -0,0 +1,37 @@ +import sys + +INF = int(1e9) + +N, M = map(int, sys.stdin.readline().split()) + +graph = [[] for _ in range(N+1)] +distance = [-1] * (N+1) +visited = [False] * (N+1) + +for _ in range(M): + a, b = map(int, sys.stdin.readline().split()) + graph[a].append(b) + +def get_biggest_node(): + max_value = -1 + index = 0 + for i in range(N+1): + if distance[i] > max_value and not visited[i]: + max_value = distance[i] + index = i + return index + +def dijkstra(start): + distance[start] = 0 + visited[start] = True + for k in graph[start]: + distance[k] = 1 + + for i in range(N-1): + now = get_biggest_node() + + visited[now] = True + for j in graph[now]: + cost = distance[now] + 1 + + diff --git a/graph2/17070/seungoh.py b/graph2/17070/seungoh.py new file mode 100644 index 0000000..f04168e --- /dev/null +++ b/graph2/17070/seungoh.py @@ -0,0 +1,14 @@ +import sys + +N = int(sys.stdin.readline()) +graph = [] + +for _ in range(N): + graph.append(list(map(lambda a: [int(a), dict({ + 'H': 0, + 'V': 0, + 'D': 0 + })], sys.stdin.readline().split()))) + +graph[0][1][1]['H'] = 1 +print(graph) diff --git a/graph2/2178/seungoh.py b/graph2/2178/seungoh.py new file mode 100644 index 0000000..4e6fdcc --- /dev/null +++ b/graph2/2178/seungoh.py @@ -0,0 +1,32 @@ +import sys +from collections import deque + +N, M = map(int, sys.stdin.readline().split()) +graph = [[] for _ in range(N)] + +for i in range(N): + x = sys.stdin.readline() + for j in range(len(x) - 1): + graph[i].append(int(x[j])) + +dx = [1, 0, -1, 0] +dy = [0, 1, 0, -1] + +def bfs(x, y): + queue = deque() + queue.append((x, y)) + while queue: + x, y = queue.popleft() + for k in range(4): + nx = x + dx[k] + ny = y + dy[k] + if nx < 0 or nx > (N-1) or ny < 0 or ny > (M-1): + continue + if graph[nx][ny] == 0: + continue + if graph[nx][ny] == 1: + queue.append((nx, ny)) + graph[nx][ny] = graph[x][y] + 1 + return graph[N-1][M-1] + +print(bfs(0,0)) \ No newline at end of file diff --git a/graph2/7562/seungoh.py b/graph2/7562/seungoh.py new file mode 100644 index 0000000..c48ee2f --- /dev/null +++ b/graph2/7562/seungoh.py @@ -0,0 +1,43 @@ +### 시간 초과 ㅠㅠ ### +import sys +from collections import deque + +def bfs(x, y): + if start == end: + return 0 + queue = deque() + queue.append((x, y)) + stop = False + while queue: + x, y = queue.popleft() + for k in range(8): + nx = x + dx[k] + ny = y + dy[k] + if nx < 0 or nx > (N-1) or ny < 0 or ny > (N-1): + continue + if graph[nx][ny] == 0: + queue.append((nx, ny)) + graph[nx][ny] = graph[x][y] + 1 + if [nx, ny] == end: + stop = True + break + if stop: + break + return graph[end[0]][end[1]] + +answers = [] +X = int(sys.stdin.readline()) + +for _ in range(X): + N = int(sys.stdin.readline()) + start = list(map(int, sys.stdin.readline().split())) + end = list(map(int, sys.stdin.readline().split())) + graph = [[0 for _ in range(N)] for _ in range(N)] + + dx = [1, 1, 2, 2, -1, -1, -2, -2] + dy = [-2, 2, -1, 1, -2, 2, -1, 1] + + answers.append(bfs(start[0], start[1])) + +for answer in answers: + print(answer) \ No newline at end of file From adee4d766dc0225fa59dd162298067db7d4a110b Mon Sep 17 00:00:00 2001 From: styu12 Date: Sat, 24 Sep 2022 13:43:36 +0900 Subject: [PATCH 2/3] solve 1780 --- .gitignore | 6 ++- divide_and_conquer/1780/seungoh1780.py | 61 ++++++++++++++++++++++++++ divide_and_conquer/1992/seungoh1992.py | 0 graph2/1325/seungoh.py | 14 ++++++ graph2/17070/seungoh.py | 2 + 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 divide_and_conquer/1780/seungoh1780.py create mode 100644 divide_and_conquer/1992/seungoh1992.py diff --git a/.gitignore b/.gitignore index 136797b..d7a5c87 100644 --- a/.gitignore +++ b/.gitignore @@ -90,4 +90,8 @@ modules.xml .idea/misc.xml *.ipr -# End of https://www.toptal.com/developers/gitignore/api/intellij+iml \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/intellij+iml + +test.js +test.py + diff --git a/divide_and_conquer/1780/seungoh1780.py b/divide_and_conquer/1780/seungoh1780.py new file mode 100644 index 0000000..a8a81a6 --- /dev/null +++ b/divide_and_conquer/1780/seungoh1780.py @@ -0,0 +1,61 @@ +import sys + +N = int(sys.stdin.readline()) +answerArr = [0, 0, 0] + +graph = [[] for _ in range(N)] + +for i in range(N): + graph[i] = list(map(int, sys.stdin.readline().split())) + +# 전체 동일한 숫자만 있는지 체크, 동일하면 해당 숫자를, 없으면 -2를 리턴 +def check_box(graph, x, y, length): + k = graph[x][y] + for i in range(x, x+length): + for j in range(y, y+length): + if graph[i][j] != k: + return -2 + return k + +# 9개의 작은 박스로 나누기. 3 나눗셈을 활용하여 어디 집어넣을지 계산 +def divide(box): + K = len(box) + division = K // 3 + small_box_list = [ + [[0 for _ in range(division)] for _ in range(division)] for _ in range(9) + ] + + for i in range(K): + for j in range(K): + x = i // division + y = j // division + box_num = 3 * x + y + box_x = i % division + box_y = j % division + small_box_list[box_num][box_x][box_y] = box[i][j] + + return small_box_list + +# 현재 박스를 체크해본뒤, 동일 숫자면 answerArr 업데이트 & 아니면 9개로 나눠서 재귀반복 실행 +def main(graph, x, y, length): + check_num = check_box(graph, x, y, length) + if check_num == -2: + division = length // 3 + main(graph, x, y, division) + main(graph, x + (division * 1), y, division) + main(graph, x + (division * 2), y, division) + main(graph, x, y + (division * 1), division) + main(graph, x + (division * 1), y + (division * 1), division) + main(graph, x + (division * 2), y + (division * 1), division) + main(graph, x, y + (division * 2), division) + main(graph, x + (division * 1), y + (division * 2), division) + main(graph, x + (division * 2), y + (division * 2), division) + + else: + answerArr[check_num+1] += 1 + + +main(graph, 0, 0, len(graph)) + +for answer in answerArr: + print(answer) \ No newline at end of file diff --git a/divide_and_conquer/1992/seungoh1992.py b/divide_and_conquer/1992/seungoh1992.py new file mode 100644 index 0000000..e69de29 diff --git a/graph2/1325/seungoh.py b/graph2/1325/seungoh.py index 549708f..62d228f 100644 --- a/graph2/1325/seungoh.py +++ b/graph2/1325/seungoh.py @@ -1,4 +1,5 @@ import sys +from collections import deque INF = int(1e9) @@ -12,6 +13,19 @@ a, b = map(int, sys.stdin.readline().split()) graph[a].append(b) +def bfs(graph, start, visited): + queue = deque([start]) + visited[start] = True + while queue: + v = queue.popleft() + if len(graph[n]) == 0: + return + for i in graph[v]: + if not visited[i]: + queue.append(i) + visited[i] = True + + def get_biggest_node(): max_value = -1 index = 0 diff --git a/graph2/17070/seungoh.py b/graph2/17070/seungoh.py index f04168e..2c8f3b0 100644 --- a/graph2/17070/seungoh.py +++ b/graph2/17070/seungoh.py @@ -12,3 +12,5 @@ graph[0][1][1]['H'] = 1 print(graph) + +## bfs 갈 수 있는 후보지는 3개 From 4e903ae3bbd03535c2d131bcb5b055a41fb266a3 Mon Sep 17 00:00:00 2001 From: styu12 Date: Sat, 24 Sep 2022 14:25:27 +0900 Subject: [PATCH 3/3] solve 18222 --- divide_and_conquer/18222/seungoh18222.py | 25 ++++++++++++++++++++++++ divide_and_conquer/1992/seungoh1992.py | 0 2 files changed, 25 insertions(+) create mode 100644 divide_and_conquer/18222/seungoh18222.py delete mode 100644 divide_and_conquer/1992/seungoh1992.py diff --git a/divide_and_conquer/18222/seungoh18222.py b/divide_and_conquer/18222/seungoh18222.py new file mode 100644 index 0000000..59b9c69 --- /dev/null +++ b/divide_and_conquer/18222/seungoh18222.py @@ -0,0 +1,25 @@ +import sys +import math + +K = int(sys.stdin.readline()) +count = 0 + +def downgrade(): + global K + global count + x = 1 + N = 0 + while N < K: + N = math.pow(2, x) + x += 1 + N = int(N // 2) + K = K - N + count += 1 + +while K != 1: + downgrade() + +if(count % 2 == 0): + print(0) +else: + print(1) \ No newline at end of file diff --git a/divide_and_conquer/1992/seungoh1992.py b/divide_and_conquer/1992/seungoh1992.py deleted file mode 100644 index e69de29..0000000