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