Advertisement
vovanhoangtuan

520B - Two Buttons

Nov 14th, 2020
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5. const int MAX = 10000+10;
  6.  
  7.  
  8. int n, m;
  9. int pre[MAX];
  10.  
  11. void task()
  12. {
  13.     bool visited[MAX];
  14.     queue<int> q;
  15.  
  16.     memset(visited, 0, sizeof(visited));
  17.  
  18.     pre[n] = -1;
  19.  
  20.     q.push(n);
  21.  
  22.     while(q.empty() == false)
  23.     {
  24.         int next = q.front();
  25.         q.pop();
  26.         if (visited[next] == false)
  27.         {
  28.             visited[next] = true;
  29.  
  30.             int subtracts = next - 1;
  31.             if (visited[subtracts] == false && subtracts >= 1 && subtracts <= 10000)
  32.             {
  33.                 pre[subtracts] = next;
  34.                 if (subtracts == m) break;
  35.                 q.push(subtracts);
  36.             }
  37.  
  38.             int multiply = next * 2;
  39.             if (visited[multiply] == false && multiply >= 1 && multiply <= 10000)
  40.             {
  41.                 pre[multiply] = next;
  42.                 if (multiply == m) break;
  43.                 q.push(multiply);
  44.             }
  45.         }
  46.     }
  47.  
  48.     int total = 0;
  49.     for (int i = pre[m]; i != -1; i = pre[i])total++;
  50.     cout << total << endl;
  51.  
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     //freopen("input.txt", "r", stdin);
  58.     cin >> n >> m;
  59.     task();
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement