Description [title]
The farmer knows the location of a cow, you want to grab it. Cattle farmers and are located on the number line, the farmer starting at point N (0 ≦ N ≤100000)
Cattle at point K (0≤ K ≤100000)
. There are two ways to move the farmer:
1, from X
Moving the X- -1 or X- + 1'd
, Take a minute each move
2, to move from X 2 × X
, Take a minute each move
Assuming that the cattle farmers are not aware of the action, stand still. The farmer takes the least amount of time to seize the cattle?
[Enter]
Two integers, N
And K
。
[Output]
An integer cattle farmer caught a minimum number of minutes you want to spend.
[Sample input]
5 17
[Sample Output]
4
// Created on 2020/2/8
/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>
#define MAXX 40*40+5
using namespace std;
//int i,j,k;
const int maxn=INT_MAX;
const int idata=100000+5;
//int aim[idata];
bool judge[idata];
bool flag;
int soux,souy,
exitx,exity;
//int maps[idata][idata];
//const int change[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
long long n,m;
long long Count[idata];
long long nx,ny;
long long x[idata];
int main()
{
long long head,tail;
int i;
cin>>n>>m;
head=0,tail=1;
x[1]=n;
judge[n]=true;
Count[n]=0;
while(head<tail)
{
head++;
for(i=0;i<3;i++)
{
switch(i)
{
case 0:nx=x[head]+1;break;
case 1:nx=x[head]-1;break;
case 2:nx=x[head]*2;break;
}
if(nx<=idata&&nx>=0&&!judge[nx])
{
judge[nx]=true;
tail++;
Count[nx]=Count[x[head]]+1;
x[tail]=nx;
}
if(nx==m)
{
flag=true;
break;
}
}
if(flag) break;
}
cout<<Count[m]<<endl;
return 0;
}