-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path섬의_개수.py
30 lines (30 loc) · 883 Bytes
/
섬의_개수.py
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
from collections import deque
d = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
while True:
n,m = list(map(int,input().split()))
if n== 0 and m == 0:
break
table = []
count = 2
for _ in range(m):
table.append(list(map(int,input().split())))
def bfs(a,b,index):
q = deque()
q.append([a,b])
table[a][b] = index
while q:
x,y = q.popleft()
for dx,dy in d:
nx,ny = x+dx,y+dy
if nx >= m or nx < 0 or ny>=n or ny < 0:
continue
if table[nx][ny] != 1:
continue
table[nx][ny] = index
q.append([nx,ny])
for ii in range(m):
for jj in range(n):
if table[ii][jj] == 1:
bfs(ii,jj,count)
count += 1
print(count-2)