Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

继续整理简单搜索

poj不支持万能头

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e5+30;
int n,m,ans;
bool vis[N];
int step[N];
int dir[3]= {1,-1,2};

void bfs(int x)
{
    queue<int>q;
    q.push(x);
    vis[x]=1;
    while(q.size())
    {
        int tmp=q.front();
        q.pop();
        for(int i=0; i<3; i++)
        {
            int xx;
            if(dir[i]!=2)
                xx=tmp+dir[i];
            else
                xx=tmp*2;
            if(xx>=0&&xx<N&&!vis[xx])
            {
                q.push(xx);
                vis[xx]=1;
                step[xx]=step[tmp]+1;
                if(xx==m)
                {
                    ans=step[xx];
                    return ;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(step,0,sizeof(step));
        memset(vis,0,sizeof(vis));
        ans=0;
        bfs(n);
        cout<<ans<<'\n';
    }
    return 0;
}