-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path토마토.py
More file actions
37 lines (32 loc) · 799 Bytes
/
토마토.py
File metadata and controls
37 lines (32 loc) · 799 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
30
31
32
33
34
35
36
37
from collections import deque
import sys
input = sys.stdin.readline
m, n = map(int, input().split())
arr = []
que = deque()
res = 0
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
for _ in range(n):
arr.append(list(map(int, input().split())))
for i in range(n):
for j in range(m):
if arr[i][j] == 1:
que.append((j , i))
def bfs():
while que:
x, y = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < m and 0 <= ny < n and arr[ny][nx] == 0:
arr[ny][nx] = arr[y][x] + 1
que.append((nx, ny))
bfs()
for i in arr:
for j in i:
if j == 0:
print(-1)
exit(0)
res = max(res, max(i))
print(res - 1)