Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long long bfs(long long n, long long m) {
  6. map<long long, long long> count;
  7. long long aux, value;
  8. queue<long long> fila;
  9. fila.push(n);
  10. count[n] = 0;
  11. while(!fila.empty()) {
  12. value = fila.front();
  13. fila.pop();
  14. cout<<value<<endl;
  15. if (value == m) return count[value];
  16.  
  17. aux = value * 2;
  18. if (count[aux] == 0) {
  19. fila.push(aux);
  20. count[aux] = count[value] + 1;
  21. }
  22. if (aux == m) return count[aux];
  23.  
  24. aux = value * 3;
  25. if (count[aux] == 0) {
  26. fila.push(aux);
  27. count[aux] = count[value] + 1;
  28. }
  29. if (aux == m) return count[aux];
  30.  
  31. aux = value / 2;
  32. if (count[aux] == 0) {
  33. fila.push(aux);
  34. count[aux] = count[value] + 1;
  35. }
  36. if (aux == m) return count[aux];
  37.  
  38. aux = value / 3;
  39. if (count[aux] == 0) {
  40. fila.push(aux);
  41. count[aux] = count[value] + 1;
  42. }
  43. if (aux == m) return count[aux];
  44.  
  45. aux = value + 7;
  46. cout<<count[value]<<endl;
  47. if (count[aux] == 0) {
  48. fila.push(aux);
  49. count[aux] = count[value] + 1, count[aux];
  50. }
  51. if (aux == m) return count[aux];
  52.  
  53. aux = value - 7;
  54. if (count[aux] == 0) {
  55. fila.push(aux);
  56. count[aux] = count[value] + 1;
  57. }
  58. if (aux == m) return count[aux];
  59. }
  60. return 0;
  61. }
  62.  
  63. int main() {
  64. long long n, m;
  65.  
  66. scanf("%lld %lld", &n, &m);
  67.  
  68. printf("%lld\n", (bfs(n, m)));
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement