알고리즘 문제풀이

백준 4964 섬의 개수 Python BFS 실버 2

아크몽 2024. 4. 4. 14:04

 

# 4964 섬의 개수 실버 2
import sys
from collections import deque
input = sys.stdin.readline

di = [0,0,1,-1,-1,1,-1,1]
dj = [1,-1,0,0,1,1,-1,-1]
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0: break
    maps = [list(map(int,input().split())) for _ in range(h)]
    visited = [[0] * (w+1) for _ in range(h)]
    cnt = 0
    # for p in maps:
    #     print(p)
    def bfs():
        global cnt
        for i in range(h):
            for j in range(w):
                if maps[i][j] == 1 and not visited[i][j]:
                    deq = deque()
                    deq.append((i,j))
                    visited[i][j] = 1
                    while deq:
                        r,c = deq.popleft()
                        for k in range(8):
                            nr = r + di[k]
                            nc = c + dj[k]
                            # 벽체크 + 방문 안한 곳 + 섬이 존재하는 곳
                            if 0 <= nr < h and 0 <= nc < w and not visited[nr][nc] and maps[nr][nc] == 1:
                                visited[nr][nc] = 1
                                deq.append((nr,nc))
                    cnt += 1
    bfs()
    print(cnt)