Hide and Seek
InterviewTime limit2sMemory limit128 MB
Compute the minimum number of moves to reach position K from N on the number line using BFS with steps +1, -1, or doubling.
Problem
Subin is playing hide-and-seek with his younger sibling.
Subin is currently at point N, and the sibling is at point K. Both positions are integers satisfying 0 <= N, K <= 100000.
In one second, Subin can either walk or teleport. If he is at position X, walking moves him to X-1 or X+1 after one second, while teleporting moves him to 2*X after one second.
Find the minimum time needed for Subin to reach the sibling.
Input
The first line contains Subin's position N and the sibling's position K, separated by a space. Both values are integers.
Output
Print the minimum number of seconds needed for Subin to reach the sibling.
Hint
When N=5 and K=17, Subin can arrive in 4 seconds by moving 5 -> 10 -> 9 -> 18 -> 17.