Baekjoon(백준)-7569 토마토(Gold 5)
baekjoon 7569 - 토마토문제입니다.
1. 문제
https://www.acmicpc.net/problem/7569
2. Input , Output
baekjoon문제는 스크린샷을 포함하지 않습니다.
3. 분류 및 난이도
Gold5
4. 문제 해석
링크 참조.
5. code
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
from collections import deque
tomato = []
M,N,H = map(int,input().split())
dq = deque()
for depth in range(H) :
tomato.append([])
for col in range(N) :
tomato[depth].append(list(map(int,sys.stdin.readline().split())))
# depth, col, row 순임.
check = [[[0 for row in range(M)]for col in range(N)] for depth in range(H)]
day = 0
tempdq = deque()
dx =[-1,0,1,0,0,0]
dy =[0,1,0,-1,0,0]
dz = [0,0,0,0,1,-1]
#초기 체크
tocount = 0
for depth in range(H) :
for row in range(M) :
for col in range(N) :
#print(depth,row,col,M,N)
if tomato[depth][col][row] == 1:
check[depth][col][row] = 1
dq.append((depth,row,col,0))
if tomato[depth][col][row] == 0 :
tocount += 1
#print(dq)
while dq :
d,r,c,counting = dq.popleft()
#print(d,r,c,counting)
for k in range(len(dz)) :
newr,newc,newd = dx[k] + r,dy[k] + c,dz[k] + d
if 0<= newd and newd < H and 0<=newr and newr<M and 0<=newc and newc < N :
if check[newd][newc][newr] == 0 and tomato[newd][newc][newr] != -1 :
if tomato[newd][newc][newr] == 0 :
tocount-=1
tempdq.append((newd,newr,newc))
dq.append((newd,newr,newc,counting+1))
check[newd][newc][newr] = 1
while tempdq :
td,tr,tc = tempdq.popleft()
tomato[td][tc][tr] = 1
day=counting
if tocount == 0 :
print(day)
else :
print(-1)
6. 결과 및 후기, 개선점
필요시 c++로 짜드립니다.
설명이 필요하다면 댓글을 달아주세요.
This post is licensed under CC BY 4.0 by the author.