Skip to content

Commit 014f365

Browse files
committed
[BOJ] #12851. 숨바꼭질2 / 골드4 / 40분 / 실패 -> 힌트 사용
1 parent 8c38a8c commit 014f365

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
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+
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

Comments
 (0)