7569 토마토

정민용·2023년 5월 18일

백준

목록 보기
219/286

문제

철수의 토마토 농장에서는 토마토를 보관하는 큰 창고를 가지고 있다. 토마토는 아래의 그림과 같이 격자모양 상자의 칸에 하나씩 넣은 다음, 상자들을 수직으로 쌓아 올려서 창고에 보관한다.

창고에 보관되는 토마토들 중에는 잘 익은 것도 있지만, 아직 익지 않은 토마토들도 있을 수 있다. 보관 후 하루가 지나면, 익은 토마토들의 인접한 곳에 있는 익지 않은 토마토들은 익은 토마토의 영향을 받아 익게 된다. 하나의 토마토에 인접한 곳은 위, 아래, 왼쪽, 오른쪽, 앞, 뒤 여섯 방향에 있는 토마토를 의미한다. 대각선 방향에 있는 토마토들에게는 영향을 주지 못하며, 토마토가 혼자 저절로 익는 경우는 없다고 가정한다. 철수는 창고에 보관된 토마토들이 며칠이 지나면 다 익게 되는지 그 최소 일수를 알고 싶어 한다.

토마토를 창고에 보관하는 격자모양의 상자들의 크기와 익은 토마토들과 익지 않은 토마토들의 정보가 주어졌을 때, 며칠이 지나면 토마토들이 모두 익는지, 그 최소 일수를 구하는 프로그램을 작성하라. 단, 상자의 일부 칸에는 토마토가 들어있지 않을 수도 있다.

# 7569
import sys
input = lambda: sys.stdin.readline().strip()

# 1. bfs
# 2. cnt_tomato

from collections import deque

m, n, h = map(int, input().split())

arr = [[list(map(int, input().split())) for _ in range(n)] for _ in range(h)]

dx, dy, dz = [1, -1, 0, 0, 0, 0], [0, 0, 1, -1, 0, 0], [0, 0, 0, 0, 1, -1]

cnt_tomato = []
for x in range(h):
    for y in range(n):
        for z in range(m):
            if arr[x][y][z] == 1:
                cnt_tomato.append((x, y, z))
                
def bfs(cnt_tomato):
    global n, m, h, arr
    
    queue = deque()
    for x, y, z in cnt_tomato:
        queue.append((x, y, z))
        
    while queue:
        x, y, z = queue.popleft()
        for i in range(6):
            nx, ny, nz = x + dx[i], y + dy[i], z + dz[i]
            if 0 <= nx < h and 0 <= ny < n and 0 <= nz < m:
                if arr[nx][ny][nz] == 0 or arr[x][y][z] + 1 < arr[nx][ny][nz]:
                    arr[nx][ny][nz] = arr[x][y][z] + 1
                    queue.append((nx, ny, nz))
                    
bfs(cnt_tomato)

day = 0
for x in range(h):
    for y in range(n):
        for z in range(m):
            if arr[x][y][z]:
                day = max(day, arr[x][y][z])
            else:
                print("-1")
                exit(0)
            
print(day-1)

백준 7569 토마토

0개의 댓글