Skip to content

Commit c2cfdf8

Browse files
authored
Merge pull request #212 from AlgorithmStudy-Allumbus/mj/presentation
feat: 5์›” 3์ฃผ์ฐจ ๊ณผ์ œ ๋ฌธ์ œ ํ’€์ด ์—…๋กœ๋“œ
2 parents 73ab8f6 + e83e6d2 commit c2cfdf8

File tree

1 file changed

+123
-1
lines changed

1 file changed

+123
-1
lines changed

โ€Ž_WeeklyChallenges/W23-[BFS]/Assignment_BOJ_7569_ํ† ๋งˆํ† .pyโ€Ž

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,126 @@
33
https://www.acmicpc.net/problem/7569
44
์œ ํ˜•: Graph, BFS
55
"""
6-
# PR ์˜ฌ๋ฆด ๋•Œ ๊ณต๊ฐœ ์˜ˆ์ •
6+
7+
"""
8+
ํ’€์ด1
9+
"""
10+
import sys
11+
from collections import deque
12+
input = sys.stdin.readline
13+
14+
# 1. ์ž…๋ ฅ ์ฒ˜๋ฆฌ
15+
M, N, H = map(int, input().split()) # ๊ฐ€๋กœ, ์„ธ๋กœ, ๋†’์ด
16+
box = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)]
17+
18+
# 2. ์ดˆ๊ธฐ ๋ณ€์ˆ˜ ์„ค์ •
19+
queue = deque([])
20+
directions = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0),
21+
(0, 0, 1), (0, 0, -1)] # ์œ„-์˜ค๋ฅธ์ชฝ-์•„๋ž˜-์™ผ์ชฝ-์•ž-๋’ค
22+
day = 0 # ์ •๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜
23+
24+
# 3. ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ† ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜๊ธฐ
25+
for i in range(H):
26+
for j in range(N):
27+
for k in range(M):
28+
if box[i][j][k] == 1:
29+
queue.append((i, j, k))
30+
31+
# 4. BFS ํƒ์ƒ‰
32+
while queue:
33+
z, y, x = queue.popleft()
34+
35+
for dx, dy, dz in directions:
36+
nx, ny, nz = x + dx, y + dy, z + dz
37+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ  ์•„์ง ์•ˆ ์ต์€ ํ† ๋งˆํ† ๋ผ๋ฉด
38+
if (0 <= nx < M and 0 <= ny < N and 0 <= nz < H) and (box[nz][ny][nx] == 0):
39+
box[nz][ny][nx] += box[z][y][x] + 1 # ์ต์€ ๋‚ ์งœ ๋ˆ„์  ๊ฐฑ์‹ 
40+
queue.append((nz, ny, nx))
41+
42+
# 5. ์ •๋‹ต ๊ตฌํ•˜๊ธฐ
43+
for height in box:
44+
for row in height:
45+
for tomato in row:
46+
# ์•ˆ ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ๋‚จ์•„์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ
47+
if tomato == 0:
48+
print(-1)
49+
exit()
50+
# ์ต๋Š”๋ฐ ๊ฑธ๋ฆฐ ์ตœ๋Œ€ ์ผ์ˆ˜ ์ถ”์ 
51+
day = max(day, max(row))
52+
53+
# 6. ์ •๋‹ต ์ถœ๋ ฅ
54+
print(day - 1)
55+
56+
57+
"""
58+
ํ’€์ด2
59+
"""
60+
import sys
61+
from collections import deque
62+
63+
input = sys.stdin.readline
64+
65+
# ์ƒ์ž์˜ ๊ฐ€๋กœ m, ์„ธ๋กœ n, ๋†’์ด h
66+
m, n, h = map(int, input().split())
67+
tomatoes = []
68+
69+
for _ in range(h):
70+
layer = [list(map(int, input().split())) for _ in range(n)]
71+
tomatoes.append(layer)
72+
73+
directions = [(0, 0, 1), (0, 0, -1), (0, -1, 0), (0, 1, 0), (1, 0, 0), (-1, 0, 0)] # ์ƒํ•˜์ขŒ์šฐ์•ž๋’ค
74+
75+
def bfs():
76+
77+
queue = deque() # (์ธต, ํ–‰, ์—ด, ์ผ์ˆ˜)
78+
79+
# ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ํ์— ์ถ”๊ฐ€
80+
for z in range(h):
81+
for x in range(n):
82+
for y in range(m):
83+
if tomatoes[z][x][y] == 1:
84+
queue.append((z, x, y, 0)) # (์ธต, ํ–‰, ์—ด, ์ผ์ˆ˜)
85+
86+
max_day = 0 # ์ต๋Š” ๋ฐ ๊ฑธ๋ฆฐ ์ตœ๋Œ€ ์ผ์ˆ˜ ์ถ”์ 
87+
88+
while queue:
89+
z, x, y, day = queue.popleft()
90+
max_day = max(max_day, day) # ๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ ์ผ์ˆ˜ ๊ฐฑ์‹ 
91+
92+
for dz, dx, dy in directions:
93+
nz, nx, ny = z + dz, x + dx, y + dy
94+
if 0 <= nz < h and 0 <= nx < n and 0 <= ny < m:
95+
if tomatoes[nz][nx][ny] == 0: # ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ์ผ ๋•Œ
96+
tomatoes[nz][nx][ny] = 1
97+
queue.append((nz, nx, ny, day + 1)) # ์ต๋Š” ๋ฐ ํ•˜๋ฃจ ์ถ”๊ฐ€
98+
99+
100+
return max_day
101+
102+
def all_ripe():
103+
# ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์—ˆ๋Š”์ง€ ํ™•์ธ
104+
for i in range(h):
105+
for j in range(n):
106+
for k in range(m):
107+
if tomatoes[i][j][k] == 0:
108+
return False
109+
return True
110+
111+
'''
112+
1: ์ต์€ ํ† ๋งˆํ† 
113+
0: ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† 
114+
-1: ํ† ๋งˆํ† ๊ฐ€ ์—†์Œ
115+
'''
116+
# ์ดˆ๊ธฐ ์ƒํƒœ ํ™•์ธ
117+
if all_ripe():
118+
print(0)
119+
exit(0)
120+
else:
121+
days = bfs()
122+
123+
# ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์—ˆ๋Š”์ง€ ๋‹ค์‹œ ํ™•์ธ
124+
if all_ripe():
125+
print(days)
126+
else:
127+
print(-1)
128+

0 commit comments

Comments
ย (0)