fork download
  1.  
  2. // i wants to take ioi
  3. //binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. #define int long long
  8. #define nn "\n"
  9. #define pi pair<int, int>
  10. #define fi first
  11. #define se second
  12. #define lb lower_bound
  13. #define ub upper_bound
  14. #define eb emplace_back
  15. #define pb push_back
  16. #define TASK ""
  17. #define ms(a, x) memset(a, x, sizeof(a))
  18. #define all(a) a.begin(), a.end()
  19. #define All(a, n) a + 1, a + 1 + n
  20. #define LOG 19
  21.  
  22. const int INF = 1e18;
  23. const int mod = 1e9;
  24. const int N = 105;
  25. const int MOD = 998244353;
  26.  
  27. int bit[200000];
  28. int n, sx, sy, vx, vy;
  29. int d[N][N];
  30. int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
  31. int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};
  32.  
  33. bool check(int x, int y) {
  34. return x >= 1 && x <= n && y >= 1 && y <= n;
  35. }
  36. void nhap() {
  37. cin >> n;
  38. cin >> sx >> sy;
  39. cin >> vx >> vy;
  40. }
  41. void solve() {
  42. ms(d, -1);
  43. queue<pi> q;
  44. q.push({sx, sy});
  45. d[sx][sy] = 0;
  46. while (!q.empty()) {
  47. auto [x, y] = q.front();
  48. q.pop();
  49. if (x == vx && y == vy) {
  50. cout << d[x][y] << nn;
  51. return;
  52. }
  53. for (int k = 0; k < 8; k++) {
  54. int nx = x + dx[k];
  55. int ny = y + dy[k];
  56. if (check(nx, ny) && d[nx][ny] == -1) {
  57. d[nx][ny] = d[x][y] + 1;
  58. q.push({nx, ny});
  59. }
  60. }
  61. }
  62. cout << 0 << nn;
  63. }
  64. signed main() {
  65. if (fopen(TASK ".INP", "r")) {
  66. freopen(TASK ".INP", "r", stdin);
  67. freopen(TASK ".OUT", "w", stdout);
  68. }
  69. ios_base::sync_with_stdio(0);
  70. cin.tie(0);
  71. cout.tie(0);
  72. nhap();
  73. solve();
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0s 544KB
stdin
10
3 3
8 8
stdout
4