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
| from collections import deque
import sys
input = sys.stdin.readline
M, N, H = map(int, input().split())
box = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)]
queue = deque([(h,r,c) for h in range(H) for r in range(N) for c in range(M) if box[h][r][c]==1])
days = 0
dz = [0,0,0,0,1,-1]
dy = [0,1,0,-1,0,0]
dx = [-1,0,1,0,0,0]
while queue:
z,y,x = queue.popleft()
days = max(days, box[z][y][x])
for i in range(6):
nz,ny,nx = z+dz[i],y+dy[i],x+dx[i]
if 0<=nz<H and 0<=ny<N and 0<=nx<M and box[nz][ny][nx]==0:
box[nz][ny][nx] = box[z][y][x] + 1
queue.append((nz,ny,nx))
if 0 in {cell for layer in box for row in layer for cell in row}:
print(-1)
else:
print(days-1)
|