Advertisement
NS2A2

Quân mã

Jul 16th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1e3 + 5;
  5.  
  6. int MoveX[8] = {-2, -2, -1, -1, 1, 1, 2, 2},
  7.     MoveY[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
  8.  
  9. int Board[maxn][maxn];
  10. int n, m, bx, by, ex, ey;
  11. void BFS(int x, int y)
  12. {
  13.     Board[x][y] = 1;
  14.  
  15.     queue<pair<int,int> > q;
  16.  
  17.     q.push(make_pair(x, y));
  18.  
  19.     while (!q.empty())
  20.     {
  21.         pair<int,int> p;
  22.         p = q.front();
  23.         q.pop();
  24.  
  25.         for(int i = 0 ; i < 8 ; i++)
  26.         {
  27.             if(1 < p.first + MoveX[i] and p.first + MoveX[i] <= n
  28.             and 1 < p.second + MoveY[i] and p.second + MoveY[i] <= m
  29.             and Board[p.first + MoveX[i]][p.second + MoveY[i]] == 0)
  30.             {
  31.                 q.push(make_pair(p.first + MoveX[i], p.second + MoveY[i]));
  32.                 Board[p.first + MoveX[i]][p.second + MoveY[i]] = Board[p.first][p.second] + 1;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.    
  41.     cin >> n >> m >> bx >> by >> ex >> ey;
  42.  
  43.     BFS(bx, by);
  44.  
  45.     cout << Board[ex][ey] - 1;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement