from collections import deque
import sys
input = sys.stdin.readline
def BFS():
while queue:
z,x,y = queue.popleft()
for dz,dx,dy in move:
nx, ny, nz = x+dx, y+dy, z+dz
if 0 <= nx < N and 0 <= ny < M and 0<= nz <H:
if board[nz][nx][ny] == 0:
board[nz][nx][ny] = board[z][x][y] + 1 # 익은 날짜+1 저장
queue.append((nz, nx, ny))
M, N, H = map(int, input().split()) # 가로, 세로, 높이
board = []
for k in range(H):
board.append([])
for _ in range(N):
board[k].append(list(map(int, input().split())))
move = [(0, 0, 1), (0, 0, -1), (1, 0, 0), (0, 1, 0), (-1, 0, 0), (0, -1, 0)]
queue = deque()
for k in range(H):
for i in range(N):
for j in range(M):
if board[k][i][j] == 1:
queue.append((k, i, j)) # 익은 토마토 큐에 삽입
BFS()
for k in range(H):
for i in range(N):
for j in range(M):
if board[k][i][j] == 0: # 덜 익은 토마토 존재
print(-1)
exit(0)
print(max(max(map(max, r)) for r in board) - 1)
이전 7576 토마토 문제 2차원에서 3차원으로 바뀜
board[][][]: 3차원 리스트로 익은 토마토=1, 0인 값은 나중에 익은 일수+1로 표시
다른 점. 상하좌우 + 위아래로 검사 필요