Skip to content

Commit 58f7ea9

Browse files
seyoung755ttasjwi
authored andcommitted
BOJ 7576 풀이 완료
1 parent 77cf171 commit 58f7ea9

1 file changed

Lines changed: 35 additions & 67 deletions

File tree

‎src/산토리/토마토.py‎

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,49 @@
1+
from collections import deque
12

2-
import sys
3+
C, R = list(map(int, input().split()))
34

4-
m, n, h = map(int, sys.stdin.readline().split())
5+
dir = [(0, 1), (0, -1), (-1, 0), (1, 0)]
56

6-
tomato = [ [ [] for _ in range(n)] for _ in range(h)]
7+
board = []
78

8-
# print(tomato[0])
9+
for _ in range(R):
10+
board.append(list(map(int, input().split())))
911

10-
for i in range(h):
11-
for j in range(n):
12-
13-
14-
t = list(map(int, sys.stdin.readline().split()))
15-
tomato[i][j] = t
12+
visited = [[False] * C for _ in range(R)]
13+
q = deque()
1614

17-
pos = [[1, 0, 0], [-1, 0, 0], [0, 0, -1], [0, 0, 1], [0, -1, 0], [0, 1, 0]]
15+
day = 0
16+
cur_day = 0
17+
for r in range(R):
18+
for c in range(C):
19+
if board[r][c] == 1:
20+
q.append((r, c, 0))
21+
visited[r][c] == True
1822

19-
def bfs(tomato):
20-
21-
cnt = 0
22-
# if cnt == 0 and 0 not in tomato:
23-
# return 0
2423

25-
while True:
26-
stack = []
27-
possible = True
24+
while q:
25+
r, c, day = q.popleft()
2826

29-
# if 0 not in tomato:
30-
# return 0
27+
for dr, dc in dir:
28+
nr, nc = r+dr, c+dc
29+
if 0 > nr or nr >= R or 0 > nc or nc >= C or visited[nr][nc] or board[nr][nc] == -1:
30+
continue
31+
if board[nr][nc] == 0:
32+
visited[nr][nc] = True
33+
cur_day = day+1
34+
q.append((nr, nc, cur_day))
35+
board[nr][nc] += 1
3136

32-
for i in range(h):
33-
for j in range(n):
34-
for k in range(m):
35-
if tomato[i][j][k] == 1:
36-
for dh, dn, dm in pos:
37-
next_h, next_n, next_m = i+dh, j+dn, k+dm
38-
if h > next_h >= 0 and n > next_n >= 0 and m > next_m >= 0:
39-
if tomato[next_h][next_n][next_m] == 0:
40-
if [next_h, next_n, next_m] not in stack:
41-
stack.append([next_h, next_n, next_m])
42-
# print(cnt, stack)
43-
elif tomato[i][j][k] == 0:
44-
possible = False
45-
if not stack:
46-
if possible:
47-
return cnt
48-
else:
49-
return -1
50-
51-
52-
53-
while stack:
54-
i, j, k = stack.pop()
55-
tomato[i][j][k] = 1
56-
57-
cnt += 1
5837

38+
day = cur_day
5939

60-
61-
# print(tomato)
62-
# break
40+
def solve():
41+
for r in range(R):
42+
for c in range(C):
43+
if board[r][c] == 0:
44+
print(-1)
45+
return
46+
print(day)
6347

6448

65-
# print(tomato[1])
66-
67-
# for i in range(h):
68-
# for j in range(n):
69-
# for k in range(m):
70-
71-
# sw = 0
72-
73-
# if tomato[i][j][k] == 1:
74-
# for dh, dn, dm in pos:
75-
# nh, nn, nm = i+dh, j+dn, k+dm
76-
# if nh >= 0 and nn >= 0 and nm >= 0:
77-
# if tomato[nh][nn][nm] == 0:
78-
# tomato[nh][nn][nm] += 1
79-
# sw = 1
80-
81-
print(bfs(tomato))
49+
solve()

0 commit comments

Comments
 (0)