-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.py
52 lines (45 loc) · 1.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Authored by : gusdn3477
# Co-authored by : tony9402
# Link : http://boj.kr/8a53cdacfc6340c894fb47257232f244
import sys
from collections import deque
def input():
return sys.stdin.readline().rstrip()
def checkMap():
for z in range(H):
for i in range(N):
for j in range(M):
if arr[z][i][j] == 0:
return False
return True
def BFS():
while queue:
q = queue.popleft()
z, x, y = q[0]
for i in range(6):
dx = x + nx[i]
dy = y + ny[i]
dz = z + nz[i]
if dx < 0 or dx >= N or dy < 0 or dy >= M or dz < 0 or dz >= H:
continue
if arr[dz][dx][dy] == 0:
arr[dz][dx][dy] = 1
queue.append(((dz,dx,dy), q[1]+1))
if checkMap():
return q[1]
return -1
M, N, H = map(int, input().split())
arr = []
nx = [-1,0,1,0,0,0]
ny = [0,-1,0,1,0,0]
nz = [0,0,0,0,-1,1]
queue = deque()
arr = [ [ list(map(int, input().split())) for _ in range(N) ] for _ in range(H) ]
for z in range(H):
for i in range(N):
for j in range(M):
if arr[z][i][j] == 1:
arr[z][i][j] = 1
queue.append(((z,i,j),0))
ans = BFS()
print(ans)