fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int OFFSET = 1020;
  5. const int MAXN = 2050;
  6.  
  7. int di[8] = {-1,-1,0,0,2,2,3,3};
  8. int dj[8] = {-1,1,-2,2,-2,2,-1,1};
  9.  
  10. int dx[]={1,-1,-2,2,-2,2,-1,1};
  11. int dy[]={2,2,1,1,-1,-1,-2,-2};
  12.  
  13. int dd[MAXN][MAXN];
  14.  
  15. void push_node(queue<pair<int,int>> &q, int u, int v, int d) {
  16. if (u < -1010 || v < -1010 || u > 1010 || v > 1010) return;
  17. if (dd[u + OFFSET][v + OFFSET]) return;
  18.  
  19. dd[u + OFFSET][v + OFFSET] = d;
  20. q.push({u, v});
  21. }
  22.  
  23. int main() {
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr);
  26.  
  27. int mx, my, tx, ty, firstMove;
  28. cin >> mx >> my >> tx >> ty >> firstMove;
  29.  
  30. queue<pair<int,int>> q;
  31.  
  32. // init
  33. if (firstMove == 1) {
  34. push_node(q, mx, my, 1);
  35. } else {
  36. for (int i = 0; i < 8; i++)
  37. push_node(q, mx + dx[i], my + dy[i], 2);
  38. }
  39.  
  40. // BFS
  41. while (!q.empty()) {
  42. auto [u, v] = q.front(); q.pop();
  43. int d = dd[u + OFFSET][v + OFFSET];
  44.  
  45. for (int i = 0; i < 8; i++) {
  46. int uu = u + di[i];
  47. int vv = v + dj[i];
  48.  
  49. if (uu == tx && vv == ty) {
  50. cout << "YES\n" << d << '\n';
  51. return 0;
  52. }
  53.  
  54. push_node(q, uu, vv, d + 1);
  55. }
  56. }
  57.  
  58. cout << "NO\n";
  59. }
  60.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
NO