Advertisement
Josif_tepe

Untitled

May 19th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int x, y;
  9. int ex, ey;
  10. cin >> x >> y;
  11. cin >> ex >> ey;
  12. if( x + y != ex + ey)
  13. {
  14. cout << -1 << endl;
  15. return 0;
  16. }
  17. int zbir = x + y;
  18. bool vis[200001];
  19. memset(vis, 0, sizeof(vis));
  20. queue <int> q, cekori;
  21. q.push(x);
  22. cekori.push(0);
  23.  
  24. while(!q.empty())
  25. {
  26. int temeX = q.front();
  27. int cek = cekori.front();
  28. q.pop();
  29. cekori.pop();
  30. if( temeX == ex )
  31. {
  32. cout << cek << endl;
  33. break;
  34. }
  35. int temeY = zbir - temeX;
  36. if( temeY - 1 > 0 && vis[temeX + 1] == false)
  37. {
  38. vis[temeX + 1] = true;
  39. q.push(temeX + 1);
  40. cekori.push(cek + 1);
  41. }
  42. if (temeX-1-((2*temeX)%31) > 0 && vis[temeX-1-((2*temeX)%31)]==false)
  43. {
  44. vis[temeX-1-((2*temeX)%31)] = true;
  45. q.push(temeX-1-((2*temeX)%31));
  46. cekori.push(cek + 1);
  47. }
  48. }
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement