Catch That Cow
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int position=100001;
int N,K;
bool visit[position];
struct pos
{
int x;
int time;
};
void bfs()
{
queue<pos>Farmer;
pos start,now,next;
memset(visit,false,sizeof(visit));
start.x=N;
start.time=0;
Farmer.push(start);
visit[start.x]=true;
while(!Farmer.empty())
{
now=Farmer.front();
Farmer.pop();
if(now.x==K)
{
cout<<now.time<<endl;
return;
}
for(int i=0;i<3;i++)
{
if(i==0)
next.x=now.x+1;
if(i==1)
next.x=now.x-1;
if(i==2)
next.x=now.x*2;
if(next.x>=0&&next.x<position&&!visit[next.x])
{
next.time=now.time+1;
Farmer.push(next);
visit[next.x]=true;
}
}
}
}
int main()
{
cin>>N>>K;
bfs();
return 0;
}