Naxocist

Knight

Apr 1st, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endll '\n'
  3. using namespace std;
  4.  
  5. int a[25][25], chk[25][25], ans = 1e9;
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(0); cin.tie(0);
  9.     int n; cin >> n;
  10.     int x, y, a, b; cin >> x >> y >> a >> b;
  11.  
  12.     queue<pair<int, int>> q;
  13.     queue<int> r;
  14.     q.push({x, y});
  15.    
  16.    
  17.     chk[x][y] = 1;
  18.     int m1[] = {1, -1};
  19.     int m2[] = {2, -2};
  20.     int curr = 0;
  21.     r.push(curr);
  22.     while(!q.empty()){
  23.         int o = q.front().first, l = q.front().second;
  24.         int round = r.front();
  25.         r.pop();
  26.         q.pop();
  27.  
  28.         for(int i=0; i<2; ++i){
  29.             for(int j=0; j<2; ++j){
  30.                 int h = o + m1[i], k = l + m2[j];
  31.                 if(h < 0 || k >= n || k<0 || h >=n) continue;
  32.  
  33.                 if(h == a && k == b){
  34.                     cout << round+1 ;
  35.                     return 0;
  36.                    
  37.                 }
  38.  
  39.                 if(chk[h][k] == 0){
  40.                     q.push({h ,k}); r.push(round+1);
  41.                     chk[h][k] = 1;
  42.                 }
  43.  
  44.                 h = o + m2[i], k = l + m1[j];
  45.                 if(chk[h][k] == 0){
  46.                     q.push({h, k});r.push(round+1);
  47.                     chk[h][k] = 1;
  48.                 }
  49.  
  50.             }
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment