We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent de5c006 commit b45ab58Copy full SHA for b45ab58
learntosurf/Graph Traversal/2024-11-22-[BOJ]-#1697-숨바꼭질.py
@@ -0,0 +1,24 @@
1
+from collections import deque
2
+import sys
3
+input = sys.stdin.readline
4
+
5
+def bfs(start, K):
6
+ queue = deque()
7
+ queue.append(start)
8
+ visited[start] = 0
9
10
+ while queue:
11
+ node = queue.popleft()
12
+ if node == K: # 동생의 위치에 도달했을 때
13
+ print(visited[node])
14
+ return
15
16
+ # 동생의 위치에 도달할 때까지, 3가지 이동 경우를 탐색
17
+ for next in (node-1, node+1, node*2):
18
+ if (0 <= next < 100001) and not visited[next]:
19
+ visited[next] = visited[node] + 1
20
+ queue.append(next)
21
22
+N, K = map(int, input().split())
23
+visited = [False] * 100001
24
+bfs(N, K)
0 commit comments