Skip to content

Commit ee610f2

Browse files
committed
[BOJ] #13549. 숨바꼭질 3 / 골드5 / 20분 / 성공
1 parent c6a3113 commit ee610f2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
MAX = 100000
6+
N, K = map(int, input().split())
7+
visited = [-1] * (MAX + 1)
8+
9+
queue = deque()
10+
queue.append(N)
11+
visited[N] = 0
12+
13+
while queue:
14+
current = queue.popleft()
15+
16+
# 순간이동 (0초) -> 큐 앞쪽에 넣기
17+
nx = current * 2
18+
if 0 <= nx <= MAX and visited[nx] == -1:
19+
visited[nx] = visited[current]
20+
queue.appendleft(nx) # 핵심!
21+
22+
# 걷는 경우 (+1, -1) -> 큐 뒤쪽에 넣기
23+
for nx in (current - 1, current + 1):
24+
if 0 <= nx <= MAX and visited[nx] == -1:
25+
visited[nx] = visited[current] + 1
26+
queue.append(nx)
27+
28+
print(visited[K])

0 commit comments

Comments
 (0)