B - Catch That Cow
src="https://vjudge.net/problem/description/8030?1516150224000" width="100%" height="988px" frameborder="0" scrolling="no" style="box-sizing: inherit;">
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n, k, temp, sum, step[100010], book[100010];
queue<int>q;
void bfs()
{
int a, b;
q.push(n);
step[n]=0;
book[n]=1;
while(!q.empty())
{
a=q.front(); q.pop();
for(int i=0;i<3;i++)
{
if(i==0)
b=a*2;
if(i==1)
b=a+1;
if(i==2)
b=a-1;
if(b<0 || b>100000)
continue;
if(!book[b])
{
q.push(b);
step[b]=step[a]+1;
book[b]=1;
}
if(b==k)
sum=step[b];
}
}
}
int main()
{
memset(book, 0, sizeof(book));
memset(step, 0, sizeof(step));
scanf("%d%d", &n, &k);
if(n>=k)
sum=n-k;
else
bfs();
printf("%d\n", sum);
return 0;
}