굉장히 쉬운 문제인데
내가 Queue를 다루는거에 좀 익숙치 않아서?
라기 보단 좀 코드 라인을 줄이겠다고 꼼수부리다가 꼬여서
엄청 헤멧지만.. 그거 아니였으면 금방 풀었을 문제
난이도가 쉽다.. 그냥 하던거 그대로 풀면됨
from queue import Queue
def BFS(x, y):
que = Queue()
que.put([x, y]) # Queue 초기화시에 put 금지
visited[x][y] = 1
while not que.empty():
tx, ty = que.get()
for i in range(len(direction)):
nx = tx + direction[i][0]
ny = ty + direction[i][1]
if 0 <= nx < h and 0 <= ny < w:
if not visited[nx][ny] and mapData[nx][ny]:
que.put([nx, ny])
visited[nx][ny] = 1
counter.append(0)
N = int(input())
for _ in range(N):
w, h, k = map(int, input().split())
mapData = [[0] * w for _ in range(h)]
visited = [[0] * w for _ in range(h)]
direction = [[-1, 0], [1, 0], [0, 1], [0, -1]]
counter = []
for _ in range(k):
y, x = map(int, input().split())
mapData[x][y] = 1
for i in range(h):
for j in range(w):
if mapData[i][j] and not visited[i][j]:
BFS(i, j)
print(len(counter))
'Algorithm > BFS, DFS' 카테고리의 다른 글
[백준] 7576번 토마토 (0) | 2020.09.14 |
---|---|
[ 백준] 2606번 바이러스 (0) | 2020.09.09 |
[백준] 2644번 촌수계산 (0) | 2020.09.08 |
[백준] 1926번 그림 (0) | 2020.09.07 |
[백준] 10809번 알파벳 찾기 (0) | 2020.09.04 |
댓글