Problem Link

#Solution

#include<bits/stdc++.h>

using namespace std;

struct point{

int x;

int tym;

};

int vis[100010];

int n,k;

#include<cstdio>

int bfs()

{memset(vis,0,sizeof vis);

point val;

val.x=n;

val.tym=0;

vis[n]=1;

queue<point>q;

point a,b,c;

q.push(val);

while(!q.empty())

{

point val=q.front();

q.pop();

if(val.x==k) return val.tym;

a.x=val.x-1;

a.tym=val.tym+1;

if(a.x >=0 && a.x<=100010&& !vis[a.x])

{

vis[a.x]=1;

q.push(a);

}

b.x=val.x+1;

b.tym=val.tym+1;

if(b.x>=0 &&b.x<=100010 &&!vis[b.x])

{

vis[b.x]=1;

q.push(b);

}

c.x=val.x*2;

c.tym=val.tym+1;

if(c.x>=0 &&c.x<=100010 &&!vis[c.x])

{

vis[c.x]=1;

q.push(c);

}

}

}

int main()

{

while(cin>>n>>k )

{

if(n>=k)

cout<<n-k<<endl;

else cout<<bfs()<<endl;

}

return 0;

}

### Like this:

Like Loading...