fork(1) download
  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. int d[100000];
  6. const int MAX = 100000;
  7. int n, m;
  8. int main() {
  9. cin >> n >> m;
  10. queue<int>q;
  11. queue<int>next_q;
  12. memset(d, -1, sizeof(d));
  13. d[n] = 0;
  14. q.push(n);
  15.  
  16. while (!q.empty()) {
  17. int x = q.front();
  18. q.pop();
  19. if (2 * x < MAX && d[2 * x] == -1) {
  20. q.push(2 * x);
  21. d[2 * x] = d[x];
  22. }
  23.  
  24. if (x - 1 >= 0 && d[x - 1] == -1) {
  25. d[x - 1] = d[x] + 1;
  26. next_q.push(x - 1);
  27. }
  28. if (x + 1 < MAX && d[x + 1] == -1) {
  29. d[x + 1] = d[x] + 1;
  30. next_q.push(x + 1);
  31. }
  32. if (q.empty()) {
  33. q = next_q;
  34. next_q = queue<int>();
  35. }
  36. }
  37. if (d[m] != -1) {
  38. cout << d[m];
  39. }
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 15632KB
stdin
50000 100000
stdout
Standard output is empty