Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. queue <pair<int,int> > q;
  9.  
  10. int main(){
  11. int n,m;
  12. cin >> n >> m;
  13. if (n==m){
  14. cout << 0 ;
  15. return 0;
  16. }
  17. pair <int,int> k ;
  18. k.first = n;
  19. k.second = 0;
  20. q.push(k);
  21. while(true){
  22. pair <int, int> k = q.front();
  23. pair <int,int> p;
  24. if (k.first*2<=10000){
  25. p.first = k.first*2;
  26. p.second = k.second+1;
  27. q.push(p);
  28. if (p.first == m){
  29. cout << p.second;
  30. return 0;
  31. }
  32. }
  33. if( k.first-1>=0){
  34. p.first = k.first-1;
  35. p.second = k.second+1;
  36. q.push(p);
  37. if (p.first==m){
  38. cout << p.second;
  39. return 0;
  40. }
  41. }
  42. q.pop();
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement