BOJ - 7569 - 토마토
Updated:
import sys
from collections import deque
def BFS(dq, tomato, N, M, K):
dx = [0, 0, 1, -1, 0, 0]
dy = [1, -1, 0, 0, 0, 0]
dz = [0, 0, 0, 0, 1, -1]
ret = 0
while dq:
ret += 1
tempDQ = deque()
while dq:
z, x, y = dq.popleft()
for step in range(6):
nx, ny, nz = x + dx[step], y + dy[step], z + dz[step]
if nx < 0 or nx >= N or ny < 0 or ny >= M or nz < 0 or nz >= K:
continue
if not tomato[nz][nx][ny]:
tomato[nz][nx][ny] = 1
tempDQ.append((nz, nx, ny))
dq = tempDQ
for i in range(N):
for j in range(M):
for k in range(K):
if tomato[k][i][j] == 0:
print(-1)
return
print(ret-1)
def solution():
M, N, K = map(int, sys.stdin.readline().split(' '))
tomato = []
for _ in range(K):
temp = []
for _ in range(N):
temp.append(list(map(int, sys.stdin.readline().split(' '))))
tomato.append(temp)
dq = deque()
for i in range(N):
for j in range(M):
for k in range(K):
if tomato[k][i][j] == 1:
dq.append((k, i, j))
BFS(dq, tomato, N, M, K)
solution()
https://www.acmicpc.net/problem/7569
Leave a comment