백준 7569 - 토마토 (파이썬)
Updated:
Answer
from sys import stdin
from collections import deque
input = stdin.readline
def bfs():
global count, queue
while queue:
x, y, z = queue.popleft()
for dx, dy, dz in (0, 1, 0), (0, -1, 0), (1, 0, 0), (-1, 0, 0), (0, 0, 1), (0, 0, -1):
nx = x + dx
ny = y + dy
nz = z + dz
if nx < 0 or nx >= M or ny < 0 or ny >= N or nz < 0 or nz >= H:
continue
if matrix[nz][ny][nx] == 0:
queue.append((nx, ny, nz))
matrix[nz][ny][nx] = matrix[z][y][x] + 1
M, N, H = map(int, input().split())
matrix = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)]
queue = deque([])
for i in range(H):
for j in range(N):
for k in range(M):
if matrix[i][j][k] == 1:
queue.append((k, j, i))
bfs()
answer = 0
for i in matrix:
for j in i:
for k in j:
if k == 0:
print(-1)
exit(0)
answer = max(answer, max(j))
print(answer - 1)