Skip to content

Commit b127f01

Browse files
committed
[BOJ]#13549. 숨바꼭질3/Gold5/실패
https://www.acmicpc.net/problem/13549
1 parent 4218b03 commit b127f01

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
MAX = 100000
5+
N , K = map(int, input().split())
6+
visited = [MAX] * (MAX+1) # 방문 여부
7+
# 2. BFS로 N-> K 의 모든 경로 찾기
8+
t=0
9+
q = deque([N , t])
10+
visited[N]= 0
11+
12+
13+
while q :
14+
cx= q.popleft()
15+
ct =visited[cx]
16+
for nx in (cx-1 , cx+1 , 2*cx) :
17+
if 0 <= nx < MAX :
18+
# 시간 업데이트
19+
if nx == 2*cx :
20+
nt = ct
21+
else :
22+
nt = ct+1
23+
24+
if visited[nx] >= MAX : # 처음 도달
25+
q.append(nx)
26+
visited[nx] = nt
27+
else : # 중복& 최단 거리일때
28+
if nt < visited[nx] :
29+
visited[nx] = nt
30+
q.append(nx)
31+
print(visited[K])

0 commit comments

Comments
 (0)