[BOJ] 7569 토마토

정지원·2020년 8월 20일
0

알고리즘

목록 보기
10/13

https://www.acmicpc.net/problem/7569

7576 토마토 의 3차원 버전이다

  • 리스트의 원소를 찾아갈 땐 zip 보다는 인덱스로 찾아가는 것이 빠르다.
  • (dx, dy, dz)와 같은 간단한(?) 것은 튜플보다는 리스트로 작성하자.
from sys import stdin
input = stdin.readline
from collections import deque

N, M, H = map(int, input().split())
arr = [[list(map(int,input()[:-1].split())) for _ in range(M)] for __ in range(H)]
dx, dy, dz = [0, -1, 0, 1, 0, 0], [1, 0, -1, 0, 0, 0],[0, 0, 0, 0, 1, -1]
queue = deque()

for i in range(N):
    for j in range(M):
        for k in range(H):
            if arr[k][j][i] == 1:
                queue.append((k,j,i))

while queue:
    this_z, this_y, this_x = queue.popleft()
    for i in range(6):
        x, y, z = this_x + dx[i], this_y + dy[i], this_z + dz[i]
        if 0 <= x < N and 0 <= y < M and 0 <= z < H:
            if arr[z][y][x] == 0:
                arr[z][y][x] = arr[this_z][this_y][this_x] + 1
                queue.append((z,y,x))

answer = 0
for j in range(M):
    for k in range(H):
        if 0 in arr[k][j]:
            print(-1)
            exit(0)
        answer = max(answer, max(arr[k][j]))
    
print(answer - 1)

0개의 댓글