fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. queue<int>q;
  4. map<int,bool>vis;
  5. vector<int>v[10001];
  6. int bfs(int m)
  7. {
  8. int ans=0;
  9. while(!q.empty())
  10. {
  11. int s=q.front();
  12. vis[s]=1;
  13. if(s==m)return ans;
  14. q.pop();
  15. if(s-1>0&&!vis[s-1])
  16. {
  17. q.push(s-1);
  18. vis[s-1]=1;
  19. ans++;
  20. }
  21. if(2*s>0&&!vis[2*s])
  22. {
  23. q.push(2*s);
  24. vis[2*s]=1;
  25. ans++;
  26. }
  27.  
  28. }
  29. return 0;
  30. }
  31. int main() {
  32. // your code goes here
  33. int n,m;
  34. cin>>n>>m;
  35. q.push(n);
  36. cout<<bfs(m);
  37. return 0;
  38. }
Success #stdin #stdout 0s 3356KB
stdin
10 1
stdout
342