-문제: https://www.acmicpc.net/problem/7569

이전에 푼거에 z가 추가된 형태.

근데도 답을 찾아봤고 답을 옮기는데도 어려움이 있었던 문제 

 

-정답풀이: 

from collections import deque

m,n,h=map(int,input().split())
s=[[] for _ in range(h)]
for i in range(h):
    for j in range(n):
        s[i].append(list(map(int,input().split())))

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

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

bfs()
k=1
result= -1
for z in s:
    for y in z:
        for x in y:
            if x==0:
                k=0
            result=max(result,x)
            
if k==0:
    print(-1)
else:
    print(result-1)

+ Recent posts