Skip to content

Commit f8623ea

Browse files
committed
[BOJ]#13549. 숨바꼭질3/ 골드5 / 1h
https://www.acmicpc.net/problem/13549
1 parent 4218b03 commit f8623ea

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Hongjoo/백준/숨박꼭질3.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
https://www.acmicpc.net/problem/13549
3+
4+
[BOJ] 숨바꼭질3/골드5/
5+
#promblem
6+
- 이동 방법[t, x] -> [t+1 , x-1] ,[t+1 , x+1] # 걷기/ ``[t , 2*x]``
7+
#flow : BFS (최단 시간 x , 최단 동작 횟수) or DFS
8+
1. node= [현 위치 x , 현 시간 t]
9+
2.이웃한 node - x+1 , x-1 , 2*x
10+
(if , nx = 2*x -> t else :걷기 -> t+1)
11+
IDEA
12+
- 같은 level -> 이동(걷기+ 순간이동) 횟수 동일함
13+
- 궁극적으로 최단 시간 = 최단 경로 , 순간이동을 가장 많이 해야함 (순간이동 우선순위가 높음)
14+
=> 2*x -> x-1, x+1 순으로 다음 node 탐색하기
15+
-> 중복되지만 순간이동으로 도달한 경우 vs 걸어서 도달한 경우를 비교할 때,
16+
"""
17+
18+
import sys
19+
from collections import deque
20+
input = sys.stdin.readline
21+
MAX = 100001
22+
N , K = map(int, input().split())
23+
visited = [MAX] * (MAX+1) # 방문 여부
24+
# 2. BFS로 N-> K 의 모든 경로 찾기
25+
q = deque([N])
26+
visited[N]= 0
27+
28+
while q :
29+
cx= q.popleft()
30+
ct = visited[cx]
31+
if cx == K:
32+
break
33+
for nx in (2*cx , cx-1 , cx+1 ) :
34+
if 0 <= nx < MAX and visited[nx] >= MAX : # nx 의 제한 조건
35+
# 시간 업데이트
36+
if nx == 2*cx :
37+
visited[nx] = visited[cx]
38+
else :
39+
visited[nx] = visited[cx] +1
40+
41+
q.append(nx)
42+
print(visited[K])

0 commit comments

Comments
 (0)