-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.py
More file actions
29 lines (23 loc) · 733 Bytes
/
D.py
File metadata and controls
29 lines (23 loc) · 733 Bytes
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
from collections import deque
import copy
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
ans = 0
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for j in range(h):
for k in range(w):
if field[j][k] == '#':
continue
d = [[float('inf')]*w for _ in range(h)]
que = deque([(j, k)])
d[j][k] = 0
x, y = j, k
while que:
x, y = que.popleft()
for i in range(4):
if 0<=x+dx[i]<h and 0<=y+dy[i]<w and field[x+dx[i]][y+dy[i]]=='.' and d[x+dx[i]][y+dy[i]] == float('inf'):
que.append((x+dx[i], y+dy[i]))
d[x+dx[i]][y+dy[i]] = d[x][y]+1
ans = max(ans, d[x][y])
print(ans)