-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7576.py
51 lines (43 loc) · 1.21 KB
/
7576.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
50
51
from collections import deque
m, n = map(int, input().split())
visited = set()
to_visit = deque()
grid = []
def set_value(arr, x, y, value):
if x < 0 or y < 0:
return False
try:
if arr[y][x] == 0:
arr[y][x] = value
else:
return False
return True
except:
return False
for i in range(n):
line = list(map(int, input().split()))
grid.append(line)
for y, line in enumerate(grid):
for x, tomato in enumerate(line):
if tomato == 1:
to_visit.append((x, y))
while to_visit:
x, y = to_visit.popleft()
if (x, y) not in visited:
visited.add((x, y))
if set_value(grid, x, y + 1, grid[y][x] + 1): #up
to_visit.append((x, y + 1))
if set_value(grid, x, y - 1, grid[y][x] + 1): # down
to_visit.append((x, y - 1))
if set_value(grid, x - 1, y, grid[y][x] + 1): # left
to_visit.append((x - 1, y))
if set_value(grid, x + 1, y, grid[y][x] + 1): # right
to_visit.append((x + 1, y))
days = 0
for line in grid:
for tomato in line:
if tomato == 0:
print(-1)
exit()
days = max(days, max(line))
print(days - 1)