Skip to content

Commit e85eb21

Browse files
authored
Feat: 6월 2주차 과제 문제 정답 풀이 업로드
1 parent 91cb31d commit e85eb21

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

_WeeklyChallenges/W26-[Graph-BFS]/Assignment_BOJ_1697_숨바꼭질.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,23 @@
44
유형: Graph, BFS
55
"""
66

7-
# PR 올릴 때 정답도 함께 공개 예정
7+
import sys
8+
from collections import deque
9+
input = sys.stdin.readline
10+
11+
MAX = 100000
12+
N, K = map(int, input().split())
13+
14+
visited = [0] * (MAX+1)
15+
queue = deque([N])
16+
17+
while queue:
18+
current = queue.popleft()
19+
if current == K:
20+
break
21+
for nx in (current + 1, current - 1, current * 2):
22+
if 0 <= nx <= MAX and not visited[nx]:
23+
visited[nx] = visited[current] + 1
24+
queue.append(nx)
25+
26+
print(visited[K])

0 commit comments

Comments
 (0)