1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
from collections import deque
graph = []
dx = [-1,1,0,0,-1,1,-1,1]
dy = [0,0,-1,1,1,-1,-1,1]
def apt(y,x):
que = deque()
que.append((x,y))
if graph[x][y]== 0:
return False
while que:
x,y = que.popleft()
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if nx<0 or ny<0 or nx>=m or ny>=n:
continue
if graph[nx][ny]==0:
continue
if graph[nx][ny]==1:
que.append((nx,ny))
graph[x][y]=0
graph[nx][ny]=0
return True
while True:
n, m = map(int, input().split())
count = 0
graph = []
if n==0 and m==0:
exit(0)
for _ in range(m):
graph.append(list(map(int,input().split())))
for h in range(m):
for w in range(n):
result=apt(w,h)
if result == True:
count += 1
print(count)
|
cs |
'백준' 카테고리의 다른 글
백준 4949번: 균형잡힌 세상 (0) | 2021.06.29 |
---|---|
백준 11866번: 요세푸스 문제 0 (0) | 2021.06.27 |
백준 1012번: 유기농 배추 (0) | 2021.05.20 |
백준 11724번: 연결 요소의 개수 (0) | 2021.05.20 |
백준 2606번: 바이러스 (0) | 2021.05.20 |