We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4218b03 commit b127f01Copy full SHA for b127f01
Hongjoo/백준/숨바꼭질3.py
@@ -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
30
31
+print(visited[K])
0 commit comments