-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_4963.py
49 lines (40 loc) · 1.02 KB
/
BOJ_4963.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
input = sys.stdin.readline
dire = [
[1, 0],
[-1, 0],
[0, -1],
[0, 1],
[1, 1],
[1, -1],
[-1, 1],
[-1, -1]
]
def dfs(hy, hx):
global land
visit[hy][hx] = True
q = [[hy, hx]]
while q:
ty, tx = q.pop()
for ny, nx in dire:
next_y, next_x = ty+ny, tx+nx
if 0 <= next_y < y and 0 <= next_x < x:
if visit[next_y][next_x] == False and graph[next_y][next_x] == 1:
visit[next_y][next_x] = True
graph[next_y][next_x] = land
q.append([next_y, next_x])
while True:
x, y = map(int, input().split())
if x == 0:
break
graph = []
for _ in range(y):
graph.append(list(map(int, input().split())))
visit = [[False for _ in range(x)] for _ in range(y)]
land = 0
for ty in range(y):
for tx in range(x):
if visit[ty][tx] == False and graph[ty][tx] == 1:
dfs(ty, tx)
land += 1
print(land)