We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8c38a8c commit 014f365Copy full SHA for 014f365
minjeong/DFSBFS/2025-06-09-[백준]-#12851-숨바꼭질2.py
@@ -0,0 +1,31 @@
1
+import sys
2
+from collections import deque
3
+
4
+input = sys.stdin.readline
5
6
+MAX = 100000
7
+N, K = map(int, input().split())
8
+dist = [-1] * (MAX + 1)
9
+ways = [0] * (MAX + 1)
10
11
+queue = deque([N])
12
+dist[N] = 0
13
+ways[N] = 1
14
15
+while queue:
16
+ x = queue.popleft()
17
18
+ for nx in (x - 1, x + 1, x * 2):
19
+ # 범위 내
20
+ if 0 <= nx <= MAX:
21
+ # 아직 방문하지 않은 위치
22
+ if dist[nx] == -1:
23
+ dist[nx] = dist[x] + 1 # 현재 걸린 시간 +1 초
24
+ ways[nx] = ways[x] # 이전 위치에서 오는 방법의 수 가져오기
25
+ queue.append(nx)
26
+ # 이미 방문했지만, 같은 시간에 다시 도달한 경우
27
+ elif dist[nx] == dist[x] + 1:
28
+ ways[nx] += ways[x] # 기존 방법 수 + 새로운 경로 수
29
30
+print(dist[K])
31
+print(ways[K])
0 commit comments