Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <cstdio>
- #include<queue>
- #include<cstring>
- using namespace std;
- int n,k;
- struct num{
- int x;
- int s;
- };
- bool vis[100005];
- void bfs()
- {
- queue<num> s;
- num start,now,next;
- memset(vis,0,sizeof(vis));
- start.x=n;
- start.s=0;
- s.push(start);
- vis[start.x]=true;
- while(!s.empty()){
- now=s.front();
- s.pop();
- if(now.x==k){
- cout<<now.s<<endl;
- return;
- }
- for(int i=0;i<3;i++){
- if(i==0){
- next.x=now.x+1;
- }else{
- if(i==1){
- next.x=now.x-1;
- }else{
- next.x=now.x*2;
- }
- }
- if(next.x>=0&&next.x<100005&&!vis[next.x]){
- vis[next.x]=true;
- next.s=now.s+1;
- s.push(next);
- }
- }
- }
- }
- int main()
- {
- cin>>n>>k;
- bfs();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement