-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path697.py
More file actions
46 lines (40 loc) · 1.16 KB
/
697.py
File metadata and controls
46 lines (40 loc) · 1.16 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
46
from collections import deque
h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
# dist[i][j] := INF -> 池でまだ見てない, -1 -> 地面で通れない
dist = [[-1] * w for _ in range(h)]
for i in range(h):
for j in range(w):
if a[i][j]:
dist[i][j] = 0
cnt = 1 # 今見ている池の番号
q = deque()
for i in range(h):
for j in range(w):
if a[i][j]:
q.append((i, j))
dist[i][j] = cnt
break
break
d = ((1, 0), (-1, 0), (0, 1), (0, -1))
for i in range(h):
for j in range(w):
if not (dist[i][j] == 0 or dist[i][j] == cnt): continue
if dist[i][j] == 0:
q.append((i, j))
while q:
vy, vx = q.popleft()
dist[vy][vx] = cnt
for dy, dx in d:
y = vy + dy
x = vx + dx
if not (0 <= x < w and 0 <= y < h): continue
if dist[y][x] != 0: continue
dist[y][x] = dist[vy][vx]
q.append((y, x))
cnt += 1
ans = 0
for i in range(h):
for j in range(w):
ans = max(ans, dist[i][j])
print(ans)