-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4963.py
More file actions
49 lines (43 loc) · 1.17 KB
/
4963.py
File metadata and controls
49 lines (43 loc) · 1.17 KB
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
38
39
40
41
42
43
44
45
#solved.ac
#실버2
#BFS
#섬의 개수
#4963
#인접 행렬 방식으로 풀이
from collections import deque
import sys
input = sys.stdin.readline
cntList = []
while True:
w,h = map(int,input().split()) #가로 세로
if w ==0 and h==0:
break
#맵 생성
maps=[[0]*(w+2)]
for i in range(h):
tmp=[0]+list(map(int,input().split()))+[0]
maps.append(tmp)
maps.append([0]*(w+2))
#BFS
visited = [[False]*(w+2) for _ in range(h+2)]
q = deque([])
cnt = 0
for i in range(1,h+1):
for j in range(1,w+1):
if not visited[i][j] and maps[i][j]==1:
q.append((i,j))
visited[i][j]=True
while q:
v = q.popleft()
for x in range(-1,2,1):
for y in range(-1,2,1):
vr = v[0]+x
vc = v[1]+y
if not visited[vr][vc] and maps[vr][vc]==1:
q.append((vr,vc))
visited[vr][vc]=True
cnt+=1
cntList.append(cnt)
#출력
for cnt in cntList:
print(cnt)