File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ # link: https://www.acmicpc.net/problem/13549
2+ # 다시 풀어볼 것
3+
4+ from collections import deque
5+
6+ def bfs ():
7+ visited = [0 ] * 100001 # i 노드까지 가는 데에 걸린 총 시간을 저장
8+ q = deque ()
9+ q .append (N )
10+
11+ while q :
12+ cur_position = q .popleft ()
13+ if cur_position == K :
14+ return visited [cur_position ]
15+ for next_position in (cur_position - 1 , cur_position + 1 , cur_position * 2 ):
16+ if 0 <= next_position < 100001 :
17+ if visited [next_position ] == 0 :
18+ if next_position == cur_position * 2 and next_position != 0 : # 순간이동일 경우 (무한루프를 피하기 위해 0 조건도 넣음)
19+ visited [next_position ] = visited [cur_position ] # 순간이동은 0초 걸림
20+ q .appendleft (next_position ) # 우선순위가 높기 때문에
21+ else :
22+ visited [next_position ] = visited [cur_position ] + 1 # 순간이동이 아닐 경우, 1초 걸림
23+ q .append (next_position ) # 우선순위가 순간이동보다 낮음
24+
25+ N , K = map (int , input ().split ())
26+ print (bfs ())
You can’t perform that action at this time.
0 commit comments