백준 문제 풀이
백준 4963 파이썬 python : 섬의 개수 @@황소처럼 우직하게@@ 짜릿짜릿
hjp845
2020. 3. 3. 00:39
반응형
import sys
input = sys.stdin.readline
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]
def bfs(y, x, count):
q = [[y, x]]
while q:
now = q.pop(0)
for i in range(8):
ny = now[0] + dy[i]
nx = now[1] + dx[i]
if 0 <= nx < w and 0 <= ny < h and mat[ny][nx] == 1:
mat[ny][nx] = count
q.append([ny, nx])
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
mat = []
for i in range(h):
mat.append(list(map(int, input().split())))
count = 2
for i in range(h):
for j in range(w):
if mat[i][j] == 1:
count += 1
bfs(i, j, count)
print(count - 2)
방향이 늘어났을 뿐
반응형