Advertisement
MaskerQwQ

I - Easy game

Mar 26th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<iostream>
  2. #include <cstdio>
  3. #include<queue>
  4. #include<cstring>
  5.  
  6. using namespace std;
  7. int n,k;
  8. struct num{
  9.     int x;
  10.     int s;
  11. };
  12. bool vis[100005];
  13. void bfs()
  14. {
  15.     queue<num> s;
  16.     num start,now,next;
  17.     memset(vis,0,sizeof(vis));
  18.     start.x=n;
  19.     start.s=0;
  20.     s.push(start);
  21.     vis[start.x]=true;
  22.     while(!s.empty()){
  23.         now=s.front();
  24.         s.pop();
  25.         if(now.x==k){
  26.             cout<<now.s<<endl;
  27.             return;
  28.         }
  29.         for(int i=0;i<3;i++){
  30.             if(i==0){
  31.                 next.x=now.x+1;
  32.             }else{
  33.                 if(i==1){
  34.                     next.x=now.x-1;
  35.                 }else{
  36.                     next.x=now.x*2;
  37.                 }
  38.             }
  39.             if(next.x>=0&&next.x<100005&&!vis[next.x]){
  40.                 vis[next.x]=true;
  41.                 next.s=now.s+1;
  42.                 s.push(next);
  43.             }
  44.         }  
  45.     }
  46. }
  47. int main()
  48. {
  49.     cin>>n>>k;
  50.     bfs();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement