fork(1) download
  1. #include <cstdio>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. queue<int> q;
  7. int d[100010];
  8.  
  9. int main()
  10. {
  11.  
  12. //freopen("ans13.txt", "r", stdin);
  13. //freopen("write13.txt","w",stdout);
  14. int n,m;
  15. scanf("%d%d",&n,&m);
  16. d[n]=0;
  17. q.push(n);
  18. while(!q.empty())
  19. {
  20. int x=q.front();q.pop();
  21. if(x==m) break;
  22. if(x<m && !d[x*2])
  23. {
  24. d[x*2]=d[x]+1;
  25. q.push(x*2);
  26. }
  27. if(x>1 && !d[x-1])
  28. {
  29. d[x-1]=d[x]+1;
  30. q.push(x-1);
  31. }
  32. }
  33. printf("%d",d[m]);
  34. return 0;
  35. }
Runtime error #stdin #stdout 0s 3208KB
stdin
Standard input is empty
stdout
Standard output is empty