[백준] 7569 - 토마토
1. 문제
2. 개요
7576 - 토마토 문제의 3차원 버전.
3. 코드 및 추가내용
import sys
from collections import deque
M, N, H = map(int, sys.stdin.readline().split())
days = 0
cnt1, cnt2, todo = 0, 0, 0
t_box = []
q = deque()
for k in range(H):
t_box.append([])
for i in range(N):
t_box[k].append(list(map(int, sys.stdin.readline().split())))
for j in range(M):
if t_box[-1][-1][j] == 1:
cnt1 += 1
q.append([i, j, k])
elif t_box[-1][-1][j] == 0:
todo += 1
dirZ, dirY, dirX = [0, 0, 0, 0, 1, -1], [-1, 0, 1, 0, 0, 0], [0, -1, 0, 1, 0, 0]
while len(q):
now = q.popleft()
cnt1 -= 1
nowY, nowX, nowZ = now[0], now[1], now[2]
for i in range(6):
nextY, nextX, nextZ = nowY + dirY[i], nowX + dirX[i], nowZ + dirZ[i]
if nextY < 0 or nextY >= N or nextX < 0 or nextX >= M or nextZ < 0 or nextZ >= H:
continue
if t_box[nextZ][nextY][nextX] != 0:
continue
cnt2 += 1
t_box[nextZ][nextY][nextX] = 1
todo -= 1
q.append([nextY, nextX, nextZ])
if cnt1 == 0 and cnt2 > 0:
cnt1 = cnt2
cnt2 = 0
days += 1
print(days) if todo == 0 else print(-1)
7576 문제와 풀이는 사실상 같다. 상자의 높이 (Z축)만 추가하면 끝.
Comments