Created
December 17, 2019 04:42
-
-
Save inspirit941/ca15a59b59c65a0ab6adc4c5a1e19668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
from copy import deepcopy | |
width, height, depth = map(int, sys.stdin.readline().split()) | |
maps = [] | |
starts = [] | |
no_zero = True | |
def bfs(starts, maps, depth, height, width): | |
# 가로, 세로, 높이로 각각 이동 | |
dirs = [(1,0,0),(-1,0,0),(0,1,0),(0,-1,0),(0,0,1),(0,0,-1)] | |
queue = deque() | |
queue.extend(starts) | |
while queue: | |
cd, ch, cw, cnt = queue.popleft() | |
for dd, dh, dw in dirs: | |
nd, nh, nw = cd + dd, ch + dh, cw + dw | |
if 0 <= nh < height and 0 <= nd < depth and 0 <= nw < width and maps[nd][nh][nw] == 0: | |
maps[nd][nh][nw] = 1 | |
queue.append((nd, nh, nw, cnt + 1)) | |
return cnt | |
# 토마토가 익을 수 있는 환경인지 아닌지 체크하는 함수 | |
def check(maps, depth, height, width): | |
for d in range(depth): | |
for h in range(height): | |
for w in range(width): | |
if maps[d][h][w] == 0: | |
return -1 | |
return 0 | |
for d in range(depth): | |
temp = [] | |
for h in range(height): | |
a = list(map(int, sys.stdin.readline().split())) | |
for w in range(width): | |
if a[w] == 0: | |
no_zero = False | |
if a[w] == 1: | |
starts.append((d,h,w,0)) | |
temp.append(deepcopy(a)) | |
maps.append(deepcopy(temp)) | |
# 처음부터 0이 하나도 없을 경우 더 계산할 필요가 없다. | |
if no_zero: | |
print(0) | |
exit(0) | |
count = bfs(starts, maps, depth, height, width) | |
if check(maps, depth, height, width) != 0: | |
print(-1) | |
else: | |
print(count) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment