-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_7569.py
99 lines (90 loc) · 2.35 KB
/
BOJ_7569.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# from collections import deque
# import sys
# input = sys.stdin.readline
# q = deque()
#
# n, m, h = map(int, input().split())
# graph = [[list(map(int, sys.stdin.readline().split())) for _ in range(n)] for _ in range(h)]
# dist = [[[0] * n for _ in range(m)] for _ in range(h)]
# isnone = True
#
# for z in range(h):
# for y in range(m):
# for x in range(n):
# if graph[z][y][x] == 1:
# q.append((z, y, x))
# isnone = False
#
# if isnone:
# print(-1)
# sys.exit(0)
#
# dx = [1, -1, 0, 0, 0, 0]
# dy = [0, 0, 1, -1, 0, 0]
# dz = [0, 0, 0, 0, 1, -1]
# while q:
# a, b, c = q.popleft()
#
# for i in range(6):
# x = c + dx[i]
# y = b + dy[i]
# z = a + dz[i]
# if 0 <= x < n and 0 <= y < m and 0 <= z < h:
# if graph[z][y][x] == 0:
# q.append((z, y, x))
# graph[z][y][x] = 1
# dist[z][y][x] = dist[a][b][c] + 1
#
# day = 0
# for z in range(h):
# for y in range(m):
# for x in range(n):
# if dist[z][y][x] > day:
# day = dist[z][y][x]
#
# elif graph[z][y][x] == 0:
# print(-1)
# sys.exit(0)
#
# print(day)
from collections import deque
import sys
input = sys.stdin.readline
q = deque()
n, m, h = map(int, input().split())
graph = [[list(map(int, sys.stdin.readline().split())) for _ in range(m)] for _ in range(h)]
dist = [[[0] * n for _ in range(m)] for _ in range(h)]
isnone = True
for z in range(h):
for y in range(m):
for x in range(n):
if graph[z][y][x] == 1:
q.append((z, y, x))
isnone = False
if isnone:
print(-1)
sys.exit(0)
dx = [1, -1, 0, 0, 0, 0]
dy = [0, 0, 1, -1, 0, 0]
dz = [0, 0, 0, 0, 1, -1]
while q:
a, b, c = q.popleft()
for i in range(6):
x = c + dx[i]
y = b + dy[i]
z = a + dz[i]
if 0 <= x < n and 0 <= y < m and 0 <= z < h:
if graph[z][y][x] == 0:
q.append((z, y, x))
graph[z][y][x] = 1
dist[z][y][x] = dist[a][b][c] + 1
day = 0
for z in range(h):
for y in range(m):
for x in range(n):
if dist[z][y][x] > day:
day = dist[z][y][x]
elif graph[z][y][x] == 0:
print(-1)
sys.exit(0)
print(day)