import sys
from collections import deque
r = sys.stdin.readline
src, dst = map(int, r().split())
visited = set()
q = deque([])
q.append((src, 0))
result = 0
while q:
cur_pos, cur_val = q.popleft()
if cur_pos == dst:
result = cur_val
break
if 0 <= cur_pos * 2 <= 100002 and cur_pos * 2 not in visited:
visited.add(cur_pos * 2)
q.appendleft((cur_pos * 2, cur_val))
if 0 <= cur_pos + 1 <= 100000 and cur_pos + 1 not in visited:
visited.add(cur_pos + 1)
q.append((cur_pos + 1, cur_val + 1))
if 0 <= cur_pos - 1 <= 100000 and cur_pos - 1 not in visited:
visited.add(cur_pos - 1)
q.append((cur_pos - 1, cur_val + 1))
print(result)
이 문제의 정해는 0-1 BFS를 사용한 풀이다.
자세한 것은 링크를 참고.
'Algorithm' 카테고리의 다른 글
7. Reverse Integer (0) | 2021.04.17 |
---|---|
104. Maximum Depth of Binary Tree (0) | 2021.04.16 |
283. Move Zeroes (0) | 2021.04.15 |
787. Cheapest Flights Within K Stops (0) | 2021.04.14 |
763. Partition Labels (0) | 2021.04.13 |