황소개발자
백준 4963 파이썬 python : 섬의 개수 @@황소처럼 우직하게@@ 짜릿짜릿 본문
반응형
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)
방향이 늘어났을 뿐
반응형
'백준 문제 풀이' 카테고리의 다른 글
백준 7576 파이썬 python : 토마토 @@황소처럼 우직하게@@ 시간초과 해결 (0) | 2020.03.03 |
---|---|
백준 2178 파이썬 python : 미로 탐색 @@황소처럼 우직하게@@ (0) | 2020.03.03 |
백준 2667 파이썬 python : 단지번호붙이기 @@황소처럼 우직하게@@ (0) | 2020.03.02 |
백준 1707 파이썬 python : 이분 그래프 @@황소처럼 우직하게@@ 이렇게 푸는겁니당 (0) | 2020.03.02 |
백준 1260 파이썬 python : DFS와 BFS @@황소처럼 우직하게@@ 깔끔 숏코딩 (1) | 2020.03.02 |
Comments