Notice
Recent Posts
Recent Comments
Link
DevKim
[Python] 백준 #4963 섬의 개수 본문
728x90
from collections import deque
import sys
def DFS(i, j):
que = deque()
if i < 0 or i >= h or j < 0 or j >= w:
return None
else:
if List[i][j] == 1:
que.append([i, j])
List[i][j] = 0
while que:
p = que.popleft()
xx = p[0]
yy = p[1]
for k in range(8):
x = dx[k]
y = dy[k]
if xx + x < 0 or xx + x >= h or yy + y < 0 or yy + y >= w:
continue
else:
if List[xx + x][yy + y] == 1:
que.append([xx + x, yy + y])
List[xx + x][yy + y] = 0
return True
else:
return None
while True:
w, h = map(int, sys.stdin.readline().rstrip().split())
if w == 0 and h == 0:
break
List = []
cnt = 0
dx = [-1, 1, 0, 0, -1, -1, 1, 1]
dy = [0, 0, -1, 1, -1, 1, -1, 1]
for i in range(h):
List.append(list(map(int, sys.stdin.readline().rstrip().split())))
for i in range(h):
for j in range(w):
k = DFS(i, j)
if k == None:
continue
else:
cnt += 1
print(cnt)
728x90
'알고리즘 PS' 카테고리의 다른 글
[Python] 백준 #9466 텀 프로젝트 (0) | 2021.01.16 |
---|---|
[Python] 백준 #1707 이분 그래프 (+) 여러가지 반례들 (0) | 2021.01.16 |
[Python] 백준 #2667 단지번호붙이기 (0) | 2021.01.16 |
[Python] 백준 #2331 반복수열 (0) | 2021.01.16 |
[Python] 백준 #2178 미로 탐색 (0) | 2021.01.16 |
Comments