From 7682dff613645375f5b5c3d0c29551e69382bdbe Mon Sep 17 00:00:00 2001 From: bsyzch Date: Thu, 8 Apr 2021 17:31:10 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=B0=AC=EB=AF=BC=20dfs/bfs=20=EB=B0=B1?= =?UTF-8?q?=EC=A4=80=20=EC=88=99=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\260\261\354\244\200_11724.py" | 28 +++++++++++++ .../\353\260\261\354\244\200_2667.py" | 39 +++++++++++++++++++ ...3\222\244\354\247\221\352\270\260_313p.py" | 2 +- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 "cmkim/BaekJoon/\353\260\261\354\244\200_11724.py" create mode 100644 "cmkim/BaekJoon/\353\260\261\354\244\200_2667.py" diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_11724.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_11724.py" new file mode 100644 index 0000000..0da4981 --- /dev/null +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_11724.py" @@ -0,0 +1,28 @@ +n, m = map(int, input().split()) +arr = [[0] * (n + 1) for _ in range(n + 1)] +visited = [0] * (n + 1) +result = 0 + +def dfs(num): + visited[num] = 1 + + for i in range(1, n+1): + if visited[i] == 0 and arr[num][i] == 1: + visited[i] = 1 + dfs(i) + return True + + + +for i in range(m): #연결리스트 + a, b = map(int, input().split()) + arr[a][b] = 1 + arr[b][a] = 1 + + +for i in range(1, n + 1): + if visited[i] == 0: + dfs(i) + result += 1 +print(result) + diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_2667.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_2667.py" new file mode 100644 index 0000000..3f6e007 --- /dev/null +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_2667.py" @@ -0,0 +1,39 @@ +from collections import deque + +n = int(input()) +arr = [[0] * n for _ in range(n)] +visited = [[0] * n for _ in range(n)] +dx, dy = [-1, 1, 0 , 0], [0, 0, -1, 1] # 상하좌우 순 +answer = [] + +def bfs(): + count = 1 + while q: + x, y = q.popleft() # x는 row, y는 col + visited[x][y] = 1 + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == 0 and arr[nx][ny] == 1: + visited[nx][ny] = 1 + q.append((nx, ny)) + count += 1 + return count + + +for i in range(n): + arr[i] = list(map(int, input().rstrip())) + +q = deque() +for i in range(n): + for j in range(n): + if arr[i][j] and visited[i][j] == 0: + q.append((i, j)) + result = bfs() + if result: + answer.append(result) + +print(len(answer)) +answer.sort() +for i in range(len(answer)): + print(answer[i]) diff --git "a/cmkim/\353\254\270\354\236\220\354\227\264\353\222\244\354\247\221\352\270\260_313p.py" "b/cmkim/\353\254\270\354\236\220\354\227\264\353\222\244\354\247\221\352\270\260_313p.py" index f16666a..c5688d4 100644 --- "a/cmkim/\353\254\270\354\236\220\354\227\264\353\222\244\354\247\221\352\270\260_313p.py" +++ "b/cmkim/\353\254\270\354\236\220\354\227\264\353\222\244\354\247\221\352\270\260_313p.py" @@ -9,4 +9,4 @@ temp = arr[0][i] count += 1 -print(count//2) \ No newline at end of file +print((count+1)//2) \ No newline at end of file From 61aab5468ff55cca65b9d3ce9b9292a60aeb1975 Mon Sep 17 00:00:00 2001 From: bsyzch Date: Fri, 9 Apr 2021 15:49:52 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EB=B0=B1=EC=A4=80=201600?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\260\261\354\244\200_1600.py" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "cmkim/BaekJoon/\353\260\261\354\244\200_1600.py" diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_1600.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_1600.py" new file mode 100644 index 0000000..6a4f82a --- /dev/null +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_1600.py" @@ -0,0 +1,49 @@ +from collections import deque + +dx, dy = [-2, -1, 1, 2, 2, 1, -1, -2], [1, 2, 2, 1, -1, -2, -2, -1] #시계방향 +dx2, dy2 = [-1, 1, 0, 0], [0, 0, -1, 1] #상하좌우 + +k = int(input()) +m, n = map(int, input().split()) # m이 가로 n이 세로 +arr = [] +visited = [[[0] * m for _ in range(n)] for _ in range(k+1)] +visited[0][0][0] = 1 + +for i in range(n): #맵 정보 입력 + arr.append(list(map(int, input().split()))) + +q = deque() +q.append((0, 0, 0, 0)) # x좌표, y좌표, 이동비용, 말이 움직인 횟수 + + +def bfs(): # x -> row | y -> col + while q: + x, y, count, action = q.popleft() # x, y 는 좌표 정보 | count는 움직인 횟수 | action은 말의 움직임을 쓴 횟수 + if x == n - 1 and y == m - 1: #목적지 도착 + print(count) + return + + for i in range(4): #상하좌우 이동 + nx = x + dx2[i] + ny = y + dy2[i] + if action >= k: #말의 움직임을 다 쓴 경우 visited에 최대 사용가능한 말의 움직임 k로 저장한다. + if 0 <= nx < n and 0 <= ny < m and visited[k][nx][ny] == 0 and arr[nx][ny] == 0: + visited[k][nx][ny] = 1 + q.append((nx, ny, count + 1, action)) + else: #말의 움직임을 다 안 쓴 경우 현재 사용한 말의 움직임을 visited에 저장 + if 0 <= nx < n and 0 <= ny < m and visited[action][nx][ny] == 0 and arr[nx][ny] == 0: + visited[action][nx][ny] = 1 + q.append((nx, ny, count + 1, action)) + + if action < k: #말의 움직임 이동 (시계방향) + for i in range(8): + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < n and 0 <= ny < m and visited[action + 1][nx][ny] == 0 and arr[nx][ny] == 0: + visited[action + 1][nx][ny] = 1 + q.append((nx, ny, count + 1, action + 1)) + return -1 #큐를 다 돌렸는데 목적지 도착해서 return 되지 않은 경우 -1 return + + +if bfs() == -1: # 말의 이동 + print(-1)