[Python] 백준7569 토마토
문제
예제 입력
코드
import sys
from collections import deque
m, n, h= map(int, input().split())
# 농장 입력
arr = [ [ [] * m for _ in range(n) ] for _ in range(h) ]
for i in range(h):
for j in range(n):
arr[i][j] = list(map(int, sys.stdin.readline().split()))
# 일수 체크
dz = [1, 0, 0, 0, 0, -1]
dy = [0, 1, 0, -1, 0, 0]
dx = [0, 0, 1, 0, -1, 0]
def BFS() :
dq = deque()
for k in range(h):
for i in range(n):
for j in range(m):
if arr[k][i][j] == 1:
dq.append([k, i, j])
while dq :
oldz, oldy, oldx = dq.popleft()
for i in range(6):
newz = oldz + dz[i]
newy = oldy + dy[i]
newx = oldx + dx[i]
if 0 <= newz < h and 0 <= newy < n and 0 <= newx < m:
if arr[newz][newy][newx] == 0:
arr[newz][newy][newx] = arr[oldz][oldy][oldx] + 1
dq.append([newz, newy, newx])
BFS()
# 확인
day = 0
for k in range(h):
for i in range(n):
for j in range(m):
day = max(day, arr[k][i][j] - 1)
if arr[k][i][j] == 0:
print(-1)
sys.exit()
print(day)
설명
파이썬을 통해서 사용자로부터 입력받아 자료형 deque를 사용하여 토마토를 구현했습니다.