Skip to content

Commit 0b119ee

Browse files
authored
Merge pull request #213 from AlgorithmStudy-Allumbus/YoonYn9915
YoonYn9915 / 5์›” 3์ฃผ์ฐจ/ 3๋ฌธ์ œ
2 parents 25d94cf + bf754a2 commit 0b119ee

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'''
2+
์š”๊ตฌ์กฐ๊ฑด
3+
4+
1. ํ† ๋งˆํ† ๋Š” ๊ฒฉ์ž ๋ชจ์–‘ ์ƒ์ž์˜ ์นธ์— ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์„œ ์ฐฝ๊ณ ์— ๋ณด๊ด€ํ•œ๋‹ค.
5+
2. ๋ณด๊ด€ ํ›„ ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚˜๋ฉด, ์ต์€ ํ† ๋งˆํ† ๋“ค์˜ ์ธ์ ‘ํ•œ ๊ณณ์— ์žˆ๋Š” ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๋“ค์€ ์ต๊ฒŒ ๋œ๋‹ค.(์ธ์ ‘ํ•œ ๊ณณ์€ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์•ž, ๋’ค๋ฅผ ์˜๋ฏธํ•œ๋‹ค)
6+
3. ์ฒ ์ˆ˜๋Š” ์ฐฝ๊ณ ์— ๋ณด๊ด€๋œ ํ† ๋งˆํ† ๋“ค์ด ๋ฉฐ์น ์ด ์ง€๋‚˜๋ฉด ๋‹ค ์ต๊ฒŒ ๋˜๋Š”์ง€, ๊ทธ ์ตœ์†Œ ์ผ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์–ด ํ•œ๋‹ค. ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์„ ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
7+
8+
1. ์•„์ด๋””์–ด
9+
- ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ตœ์†Œ ์ผ์ˆ˜๋ฅผ ์•Œ์•„์•ผ ํ•˜๋ฏ€๋กœ BFS
10+
- ๊ฐ ํ† ๋งˆํ† ๊ฐ€ ์ต๋Š” ์ผ์ˆ˜๋Š” ์ธ์ ‘ํ•œ ํ† ๋งˆํ† ๊ฐ€ ์ต์€ ์ผ์ˆ˜ + 1์ด๋ฏ€๋กœ visited ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด ํ† ๋งˆํ† ๊ฐ€ ์ต๋Š” ์ผ ์ €์žฅ
11+
12+
2. ์‹œ๊ฐ„๋ณต์žก๋„
13+
- 2 โ‰ค M,N โ‰ค 1,000
14+
- BFS์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(V+E) V๋Š” ์ตœ๋Œ€ 1,000,000, E๋Š” ์ตœ๋Œ€ 4,000,000. 1์ดˆ ์•ˆ์— ๊ฐ€๋Šฅ
15+
16+
3. ๊ตฌํ˜„
17+
1. ์ž…๋ ฅ๋ฐ›๊ธฐ
18+
2. ์ดˆ๊ธฐ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ 1์ด๊ฑฐ๋‚˜ -1์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
19+
3. bfs๋กœ ํ† ๋งˆํ†  ์ตํžˆ๊ธฐ ์‹œ๋ฎฌ๋ ˆ์ด์…˜
20+
4. ๋ชจ๋‘ 1์ด ์•„๋‹Œ๋ฐ bfs๊ฐ€ ๋๋‚˜๋ฉด -1 ๋ฐ˜ํ™˜, ๋ชจ๋‘ 1์ด๋ฉด visited ๋ฐฐ์—ด ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’(์ตœ์†Œ ์ผ์ˆ˜) ์ถœ๋ ฅ.
21+
'''
22+
23+
import sys
24+
from collections import deque
25+
26+
inp = sys.stdin.readline
27+
28+
M, N = map(int, inp().split())
29+
30+
arr = []
31+
visited = [[-1] * M for _ in range(N)]
32+
33+
for _ in range(N):
34+
arr.append(list(map(int, inp().split())))
35+
36+
37+
def find_tomato(arr, visited):
38+
tomatoes = []
39+
for i in range(N):
40+
for j in range(M):
41+
if arr[i][j] == 1:
42+
tomatoes.append((i, j))
43+
visited[i][j] = 0
44+
return tomatoes
45+
46+
47+
def bfs(arr, visited):
48+
dx = [1, -1, 0, 0]
49+
dy = [0, 0, 1, -1]
50+
51+
# ์ดˆ๊ธฐ ์„ค์ •
52+
queue = deque(find_tomato(arr, visited))
53+
54+
# BFS
55+
while queue:
56+
x, y = queue.popleft()
57+
for i in range(4):
58+
new_x = x + dx[i]
59+
new_y = y + dy[i]
60+
61+
# ์ƒ์ž ์•ˆ์ด๊ณ , 0์ด๊ณ , ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
62+
if 0 <= new_x < N and 0 <= new_y < M and arr[new_x][new_y] == 0 and visited[new_x][new_y] == -1:
63+
# 1๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•˜๊ณ  queue์— ๋„ฃ๋Š”๋‹ค
64+
arr[new_x][new_y] = 1
65+
visited[new_x][new_y] = visited[x][y] + 1
66+
queue.append((new_x, new_y))
67+
68+
def check_tomatoes(arr):
69+
for i in range(N):
70+
for j in range(M):
71+
if arr[i][j] not in [-1, 1]:
72+
return True
73+
return False
74+
75+
# BFS๊ฐ€ ๋๋‚œ ํ›„์— ๋ชจ๋‘ 1์ด ์•„๋‹ˆ๋ฉด -1 ๋ฐ˜ํ™˜, ๋ชจ๋‘ 1์ด๋ฉด visited ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’ ์ฐพ๊ธฐ
76+
if check_tomatoes(arr):
77+
print(-1)
78+
else:
79+
print(max(map(max, visited)))
80+
81+
flag = False
82+
83+
# ์ดˆ๊ธฐ์— ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ 1์ด๊ฑฐ๋‚˜ -1์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
84+
for i in range(N):
85+
for j in range(M):
86+
if arr[i][j] == 0:
87+
flag = True
88+
break
89+
90+
if flag:
91+
bfs(arr, visited)
92+
else:
93+
print(0)
94+
95+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'''
2+
์š”๊ตฌ์กฐ๊ฑด
3+
1. ์ต์€ ํ† ๋งˆํ† ๋“ค์˜ ์ธ์ ‘ํ•œ ๊ณณ์— ์žˆ๋Š” ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๋“ค์€ ์ต์€ ํ† ๋งˆํ† ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ต๊ฒŒ ๋œ๋‹ค.
4+
ํ•˜๋‚˜์˜ ํ† ๋งˆํ† ์— ์ธ์ ‘ํ•œ ๊ณณ์€ ์œ„, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์•ž, ๋’ค ์—ฌ์„ฏ ๋ฐฉํ–ฅ์— ์žˆ๋Š” ํ† ๋งˆํ† ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
5+
6+
7+
2. ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์„ ๋•Œ๊นŒ์ง€ ์ตœ์†Œ ๋ฉฐ์น ์ด ๊ฑธ๋ฆฌ๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
8+
๋งŒ์•ฝ, ์ €์žฅ๋  ๋•Œ๋ถ€ํ„ฐ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์–ด์žˆ๋Š” ์ƒํƒœ์ด๋ฉด 0์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ณ ,
9+
ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์ง€๋Š” ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด๋ฉด -1์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
10+
11+
1. ์•„์ด๋””์–ด
12+
- ์ต์€ ํ† ๋งˆํ† ์˜ ์ธ์ ‘ํ•œ ๊ณณ์œผ๋กœ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ bfs ์‚ฌ์šฉ.
13+
- ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ตœ์†Œ ๋ช‡ ์ผ์— ์ต์—ˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด์„œ ํ† ๋งˆํ† ๊ฐ€ ์ต์€ ์ผ์ˆ˜๋ฅผ ์ €์žฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ visited ๋ฐฐ์—ด์— ์ €์žฅ.
14+
15+
2. ์‹œ๊ฐ„๋ณต์žก๋„
16+
- BFS์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(V+E) V๋Š” ์ตœ๋Œ€ 1,000,000, E๋Š” ์ตœ๋Œ€ 6,000,000. 1์ดˆ ์•ˆ์— ๊ฐ€๋Šฅ
17+
18+
3. ๊ตฌํ˜„
19+
3-1. ์ž…๋ ฅ๋ฐ›๊ธฐ
20+
3-2. ์ดˆ๊ธฐ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ 1 ํ˜น์€ -1์ธ์ง€ ํ™•์ธ.
21+
3-3. bfs ์‹คํ–‰
22+
3-4. ๋ชจ๋‘ 1์ด ์•„๋‹Œ๋ฐ bfs๊ฐ€ ๋๋‚˜๋ฉด -1 ๋ฐ˜ํ™˜, ๋ชจ๋‘ 1์ด๋ฉด visited ๋ฐฐ์—ด ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’(์ตœ์†Œ ์ผ์ˆ˜) ์ถœ๋ ฅ.
23+
24+
'''
25+
26+
from collections import deque
27+
import sys
28+
29+
inp = sys.stdin.readline
30+
31+
# ๊ฐ€๋กœ, ์„ธ๋กœ, ๋†’์ด
32+
N, M, H = map(int, inp().strip().split())
33+
34+
arr = []
35+
visited = [[[-1] * N for _ in range(M)] for _ in range(H)]
36+
37+
# ์ž…๋ ฅ๋ฐ›๊ธฐ
38+
for _ in range(H):
39+
temp = []
40+
for _ in range(M):
41+
temp.append(list(map(int, inp().strip().split())))
42+
arr.append(temp)
43+
44+
flag = False
45+
# ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ† ๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
46+
tomatoes = []
47+
48+
# ์ดˆ๊ธฐ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ 1 ํ˜น์€ -1์ธ์ง€ ํ™•์ธ.
49+
for i in range(H):
50+
for j in range(M):
51+
for k in range(N):
52+
if arr[i][j][k] == 0:
53+
flag = True
54+
break
55+
if flag:
56+
break
57+
if flag:
58+
break
59+
60+
# ์ดˆ๊ธฐ 1์ธ ํ† ๋งˆํ†  ์ €์žฅ
61+
for i in range(H):
62+
for j in range(M):
63+
for k in range(N):
64+
if arr[i][j][k] == 1:
65+
tomatoes.append((i, j, k))
66+
visited[i][j][k] = 0
67+
68+
69+
def bfs(arr, visited, tomatoes):
70+
# ๋จผ์ € ์ต์€ ํ† ๋งˆํ† ๋“ค๋กœ ํ ์ดˆ๊ธฐํ™”
71+
queue = deque(tomatoes)
72+
73+
# ์ƒ ํ•˜ ์ขŒ ์šฐ ์œ„ ์•„๋ž˜
74+
dx = [-1, 1, 0, 0, 0, 0]
75+
dy = [0, 0, -1, 1, 0, 0]
76+
dz = [0, 0, 0, 0, -1, 1]
77+
78+
# bfs ํƒ์ƒ‰
79+
while queue:
80+
(z, y, x) = queue.popleft()
81+
for idx in range(6):
82+
# ์ธ์ ‘ ์นธ์œผ๋กœ ์ด๋™
83+
new_z = z + dz[idx]
84+
new_y = y + dy[idx]
85+
new_x = x + dx[idx]
86+
87+
# ์กฐ๊ฑด ํ™•์ธ(1. ์ธ์ ‘์นธ์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋ฒ”์œ„ ์•ˆ์ธ์ง€, 2. ์ธ์ ‘์นธ์ด ์•ˆ์ต์€ ํ† ๋งˆํ† ์ธ์ง€, 3. ์ธ์ ‘์นธ์ด ๋ฐฉ๋ฌธํ•œ์  ์—†๋Š”์ง€)
88+
if (0 <= new_x < N and 0 <= new_y < M and 0 <= new_z < H) and (arr[new_z][new_y][new_x] == 0) and \
89+
(visited[new_z][new_y][new_x] == -1):
90+
# ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
91+
queue.append((new_z, new_y, new_x))
92+
arr[new_z][new_y][new_x] = 1
93+
visited[new_z][new_y][new_x] = visited[z][y][x] + 1
94+
95+
# bfs๊ฐ€ ๋๋‚ฌ์„ ๋•Œ, arr ๋ฐฐ์—ด์— 0์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด -1 ์ถœ๋ ฅ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด visited ๋ฐฐ์—ด ์š”์†Œ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’ ์ถœ๋ ฅ
96+
if any(item == 0 for depth in arr for row in depth for item in row):
97+
print(-1)
98+
else:
99+
print(max(item for depth in visited for row in depth for item in row))
100+
101+
102+
if flag:
103+
bfs(arr, visited, tomatoes)
104+
else:
105+
print(0)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
1. ์•„์ด๋””์–ด
3+
- ์ƒ์ด ์™•์—๊ฒŒ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์žฅ๊ธฐํŒ์„ ๊ทธ๋ž˜ํ”„๋กœ ๋†“๊ณ  ์ƒ์ด ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋ฅผ bfs๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
4+
5+
2. ์‹œ๊ฐ„๋ณต์žก๋„
6+
- BFS ์‹œ๊ฐ„๋ณต์žก๋„ O(V + E), V๋Š” 10 * 9 , E๋Š” ์ตœ๋Œ€ 4 * 10 * 9.
7+
8+
3. ๊ตฌํ˜„
9+
3.1 ์ž…๋ ฅ๋ฐ›๊ธฐ
10+
3.2 ์ƒ์˜ ์ดˆ๊ธฐ ์œ„์น˜์—์„œ๋ถ€ํ„ฐ 8๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ ๋‚˜์•„๊ฐ€๋ฉฐ bfs ์‹คํ–‰.
11+
3.3 ์ด๋™ ๊ฒฝ๋กœ์— ์™•์ด ์žˆ์œผ๋ฉด ์ด๋™ ๋ถˆ๊ฐ€
12+
3.4 bfs๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ์™•์˜ ์œ„์น˜์— ๋„๋‹ฌํ•˜๋ฉด ์ด๋™ํšŸ์ˆ˜ ์ถœ๋ ฅ, ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜๋ฉด -1 ์ถœ๋ ฅ
13+
'''
14+
15+
from collections import deque
16+
17+
# ์ž…๋ ฅ ๋ฐ›๊ธฐ
18+
R1, C1 = map(int, input().split()) # ์ƒ์˜ ์‹œ์ž‘ ์œ„์น˜
19+
R2, C2 = map(int, input().split()) # ์™•์˜ ์œ„์น˜
20+
21+
# ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ -1๋กœ ์ดˆ๊ธฐํ™” (10ํ–‰ x 9์—ด)
22+
visited = [[-1] * 9 for _ in range(10)]
23+
visited[R1][C1] = 0 # ์‹œ์ž‘ ์œ„์น˜๋Š” 0์œผ๋กœ ํ‘œ์‹œ
24+
25+
# ์ƒ์˜ 8๊ฐ€์ง€ ์ด๋™ ๋ฐฉํ–ฅ ๋ฐ ๊ทธ์— ๋”ฐ๋ฅธ ๊ฒฝ์œ  ์ขŒํ‘œ ์ •์˜
26+
# ๊ฐ๊ฐ: ๊ฒฝ์œ 1, ๊ฒฝ์œ 2, ์ตœ์ข… ๋ชฉ์ ์ง€ (์ด 3๋‹จ๊ณ„ ์ด๋™)
27+
paths = [
28+
[(-1, 0), (-2, -1), (-3, -2)],
29+
[(-1, 0), (-2, 1), (-3, 2)],
30+
[(1, 0), (2, -1), (3, -2)],
31+
[(1, 0), (2, 1), (3, 2)],
32+
[(0, -1), (-1, -2), (-2, -3)],
33+
[(0, -1), (1, -2), (2, -3)],
34+
[(0, 1), (-1, 2), (-2, 3)],
35+
[(0, 1), (1, 2), (2, 3)],
36+
]
37+
38+
# ์ด๋™ ๊ฒฝ๋กœ์— ์™•์ด ์žˆ๋Š”์ง€ ํ™•์ธ
39+
def check(x, y, i):
40+
for dx, dy in paths[i][:2]: # ๊ฒฝ์œ ์ง€ ๋‘ ๊ณณ๋งŒ ํ™•์ธ
41+
mx, my = x + dx, y + dy
42+
if mx == R2 and my == C2:
43+
return False
44+
return True
45+
46+
# BFS๋กœ ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜ ์ฐพ๊ธฐ
47+
def bfs():
48+
queue = deque([(R1, C1)])
49+
50+
while queue:
51+
x, y = queue.popleft()
52+
53+
# ์™•์˜ ์œ„์น˜์— ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ
54+
if x == R2 and y == C2:
55+
print(visited[x][y])
56+
return
57+
58+
for i in range(8):
59+
nx = x + paths[i][2][0]
60+
ny = y + paths[i][2][1]
61+
62+
# ๋ฒ”์œ„ ์•ˆ์ด๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉฐ, ๊ฒฝ๋กœ์— ์™•์ด ์—†๋‹ค๋ฉด
63+
if 0 <= nx < 10 and 0 <= ny < 9 and visited[nx][ny] == -1 and check(x, y, i):
64+
visited[nx][ny] = visited[x][y] + 1
65+
queue.append((nx, ny))
66+
67+
# ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
68+
print(-1)
69+
70+
bfs()

0 commit comments

Comments
ย (0)