-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3차원_토마토.py
61 lines (47 loc) · 1.52 KB
/
3차원_토마토.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
# https://www.acmicpc.net/problem/7569
# 이번문제를 풀면서 알게된 정보
# sys.stdin.readline() 함수를 쓰면 더 빨리 읽을 수 있다.
# max함수의 경우 다차원 배열의 경우 맨앞의 원소값을 비교하여 대소를 판단한다.(값이 같으면 다음원소를 비교)
import sys
from collections import deque
def bfs(queue,box,m,n,h):
dx = [1,0,0,-1,0,0]
dy = [0,1,0,0,-1,0]
dz = [0,0,1,0,0,-1]
while queue:
ori_z,ori_y,ori_x = queue.popleft()
for i in range(6):
x = ori_x + dx[i]
y = ori_y + dy[i]
z = ori_z + dz[i]
if 0<=x<m and 0<=y<n and 0<=z<h:
if box[z][y][x] == 0:
queue.append((z,y,x))
box[z][y][x] = box[ori_z][ori_y][ori_x] + 1
m,n,h = map(int,input().split())
box = [[] for _ in range(h)]
for i in range(h):
for j in range(n):
box[i].append(list(map(int,sys.stdin.readline().split())))
queue = deque()
flag = 0 #0이 유지 될경우 모든 토마토가 익은 경우
for z in range(h):
for y in range(n):
for x in range(m):
if box[z][y][x] == 1:
queue.append((z,y,x))
if box[z][y][x] == 0:
flag = 1
answer = 0
if flag == 0:
print(0)
else:
bfs(queue,box,m,n,h)
for a in box:
for b in a:
for c in b:
if c == 0:
print(-1)
exit(0)
answer = max(answer,max(b))
print(answer-1)