728x90
반응형
문제
https://www.acmicpc.net/problem/4963
문제 풀이
import collections
import sys
dx = [-1, 0, 1, 1, 1, 0, -1, -1]
dy = [-1, -1, -1, 0, 1, 1, 1, 0]
def bfs(graph, y, x):
graph[y][x] = 0
queue = collections.deque([(y, x)])
while queue:
yy, xx = queue.popleft()
for i in range(8):
nx = xx + dx[i]
ny = yy + dy[i]
if nx < 0 or nx >= w or ny < 0 or ny >= h:
continue
if graph[ny][nx] == 1:
graph[ny][nx] = 0
queue.append((ny, nx))
while True:
w, h = map(int, sys.stdin.readline().split())
if w == 0 and h == 0:
break
maps = [list(map(int, sys.stdin.readline().split())) for _ in range(h)]
count = 0
for y in range(h):
for x in range(w):
if maps[y][x] == 1:
bfs(maps, y, x)
count += 1
print(count)
728x90
반응형
'IT > 알고리즘' 카테고리의 다른 글
[백준] 2468 파이썬(python) (0) | 2022.01.08 |
---|---|
[백준] 1697 파이썬(python) (0) | 2022.01.08 |
[백준] 11403 파이썬(python) (0) | 2022.01.08 |
[백준] 9372 파이썬(python) (0) | 2022.01.08 |
[백준] 7569 파이썬(python) (0) | 2022.01.08 |