Skip to content

Commit 693043f

Browse files
committed
[BOJ] 숨바꼭질 3 / 골드 5 / 40분
https://www.acmicpc.net/problem/13549
1 parent 51ffa80 commit 693043f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
from collections import deque
3+
4+
inp = sys.stdin.readline
5+
6+
N, K = map(int, inp().strip().split())
7+
# 해당 위치로 도달한 최소 시간 저장
8+
visited = [-1] * (100_000 + 1)
9+
visited[N] = 0
10+
11+
# (위치, 시간) 형식
12+
queue = deque()
13+
queue.append((N, 0))
14+
15+
dx = [2, -1, 1]
16+
17+
min_time = -1
18+
19+
while queue:
20+
subin_loc, time = queue.popleft()
21+
22+
# 수빈이가 동생에게 도달한 시간까지만 bfs탐색하고 그 후에 종료
23+
if min_time != -1 and min_time + 2 == time:
24+
break
25+
26+
# 수빈이가 동생에게 도달한 시간체크
27+
if min_time == -1 and subin_loc == K:
28+
min_time = visited[subin_loc]
29+
30+
# 수빈의 위치에서 3가지 이동
31+
for i in range(3):
32+
if i == 0:
33+
new_loc = subin_loc * dx[i]
34+
else:
35+
new_loc = subin_loc + dx[i]
36+
37+
# 새 위치가 범위 안
38+
if 0 <= new_loc <= 100_000:
39+
# 새로 방문한 위치가 이전에 와보지 못했다면,
40+
if visited[new_loc] == -1:
41+
# 방문 시간 설정해주고 경로 초기화
42+
if i == 0:
43+
visited[new_loc] = visited[subin_loc]
44+
queue.append((new_loc, time))
45+
else:
46+
visited[new_loc] = visited[subin_loc] + 1
47+
queue.append((new_loc, time + 1))
48+
49+
50+
print(visited[K])

0 commit comments

Comments
 (0)