fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n,k;
  6. int cnt[100001]={0,};
  7. int dx[3]={-1,1,2};
  8. queue<int>q;
  9.  
  10. int bfs(int i){
  11. q.push(i);
  12.  
  13. while(!q.empty()){
  14. int x=q.front();
  15. q.pop();
  16.  
  17. if(x==k)
  18. return cnt[x];
  19.  
  20. for(int i=0;i<3;i++){
  21. int nx=x+dx[i];
  22. if(i==2)
  23. nx=2*x;
  24.  
  25. if(nx>=0 && nx<=100000 && cnt[nx]==0){
  26. cnt[nx]=cnt[x]+1;
  27. q.push(nx);
  28. }
  29. }
  30. }
  31. }
  32.  
  33. int main() {
  34. cin>>n>>k;
  35.  
  36. cout<<bfs(n);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5324KB
stdin
5 17
stdout
4