Advertisement
coloriot

HA30_Horse(3)

Nov 8th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. class Chessboard {
  8. private:
  9.     int N;
  10.     vector<vector<bool> > used;                        
  11.     vector<vector<pair<int, int> > > prev;            
  12.     int dx[8];                                        
  13.     int dy[8];                                        
  14.  
  15. public:
  16.     Chessboard(int size) {
  17.         N = size;
  18.         used.resize(N, vector<bool>(N, false));
  19.         prev.resize(N, vector<pair<int, int> >(N, make_pair(-1, -1)));
  20.  
  21.         // Инициализация смещений для хода коня
  22.         int temp_dx[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
  23.         int temp_dy[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  24.         for (int i = 0; i < 8; ++i) {
  25.             dx[i] = temp_dx[i];
  26.             dy[i] = temp_dy[i];
  27.         }
  28.     }
  29.  
  30.     bool is_valid(int x, int y) {
  31.         return x >= 0 && x < N && y >= 0 && y < N;
  32.     }
  33.  
  34.     int BFS(int x1, int y1, int x2, int y2) {
  35.         queue<pair<int, int> > q;
  36.         q.push(make_pair(x1, y1));
  37.         used[x1][y1] = true;
  38.  
  39.         while (!q.empty()) {
  40.             pair<int, int> cur = q.front();
  41.             q.pop();
  42.             int x = cur.first;
  43.             int y = cur.second;
  44.  
  45.             if (x == x2 && y == y2) {
  46.                 return 0; // Мы достигли цели
  47.             }
  48.  
  49.             for (int i = 0; i < 8; ++i) {
  50.                 int nx = x + dx[i];
  51.                 int ny = y + dy[i];
  52.  
  53.                 if (is_valid(nx, ny) && !used[nx][ny]) {
  54.                     used[nx][ny] = true;
  55.                     prev[nx][ny] = make_pair(x, y);
  56.                     q.push(make_pair(nx, ny));
  57.                 }
  58.             }
  59.         }
  60.         return -1;
  61.     }
  62.  
  63.     vector<pair<int, int> > get_path(int x1, int y1, int x2, int y2) {
  64.         vector<pair<int, int> > path;
  65.         int x = x2;
  66.         int y = y2;
  67.  
  68.         while (!(x == x1 && y == y1)) {
  69.             path.push_back(make_pair(x, y));
  70.             pair<int, int> p = prev[x][y];
  71.             x = p.first;
  72.             y = p.second;
  73.         }
  74.         path.push_back(make_pair(x1, y1));
  75.  
  76.         // Разворачиваем путь
  77.         vector<pair<int, int> > reversed_path;
  78.         for (int i = path.size() - 1; i >= 0; --i) {
  79.             reversed_path.push_back(path[i]);
  80.         }
  81.         return reversed_path;
  82.     }
  83.  
  84.     int get_move_count(int x1, int y1, int x2, int y2) {
  85.         int moves = 0;
  86.         int x = x2;
  87.         int y = y2;
  88.  
  89.         while (!(x == x1 && y == y1)) {
  90.             pair<int, int> p = prev[x][y];
  91.             x = p.first;
  92.             y = p.second;
  93.             moves++;
  94.         }
  95.         return moves;
  96.     }
  97. };
  98.  
  99. int main() {
  100.     int N, x1, y1, x2, y2;
  101.     cin >> N >> x1 >> y1 >> x2 >> y2;
  102.  
  103.     x1--; y1--;
  104.     x2--; y2--;
  105.  
  106.     Chessboard chessboard(N);
  107.     chessboard.BFS(x1, y1, x2, y2);
  108.  
  109.     vector<pair<int, int> > path = chessboard.get_path(x1, y1, x2, y2);
  110.     int move_count = path.size() - 1;
  111.  
  112.     cout << move_count << endl;
  113.     for (size_t i = 0; i < path.size(); ++i) {
  114.         cout << path[i].first + 1 << " " << path[i].second + 1 << endl;
  115.     }
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement