[BOJ] 7569. ํ ๋งํ (python)
๐๋ฌธ์
๐ช์์ด๋์ด
์์ด๋์ด๋ ์ด๋ฌธ์ ์ ๊ฐ๋ค. ์ด์ฐจ์์ ์ผ์ฐจ์์ผ๋ก ํ์ฅํ์ ๋ฟ์ด๋ค.
๐ฅ์ฝ๋
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
import sys
from collections import deque
input = sys.stdin.readline
m,n,h = map(int,input().split())
ground=[]
unripe = 0
for _ in range(h):
tmp=[]
for _ in range(n):
tmp.append(list(map(int,input().split())))
ground.append(list(tmp))
queue=deque()
for z in range(h):
for i in range(n):
for j in range(m):
if ground[z][i][j]==1:
queue.append((z,i,j,0))
if ground[z][i][j]==0:
unripe+=1
if unripe==0:
print(0)
exit(0)
visited = []
for _ in range(h):
visited.append([[False for _ in range(m)]for _ in range(n)])
dir = [(0,-1,0),(0,0,1),(0,1,0),(0,0,-1),(1,0,0),(-1,0,0)]
while queue:
nz, ny, nx, day = queue.popleft()
for d in dir:
ez, ey, ex = nz + d[0], ny + d[1], nx + d[2]
if 0<=ez<h and 0<=ey<n and 0<= ex<m:
if not visited[ez][ey][ex] and ground[ez][ey][ex]==0:
visited[ez][ey][ex] = True
ground[ez][ey][ex]==1
unripe-=1
if unripe==0:
print(day+1)
exit(0)
queue.append((ez,ey,ex,day+1))
print(-1)
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.