Advertisement
coloriot

HA30_Races(6)

Nov 8th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. class Grid {
  8. private:
  9.     int n, m;
  10.     vector<vector<char> > grid;
  11.     vector<vector<int> > visited;
  12.  
  13.     // Смещения для движений: вверх, вниз, влево, вправо
  14.     int dx[4];
  15.     int dy[4];
  16.  
  17.     int sx, sy;
  18.     int tx, ty;
  19.  
  20. public:
  21.     Grid(int n_rows, int m_cols) {
  22.         n = n_rows;
  23.         m = m_cols;
  24.         grid.resize(n, vector<char>(m));
  25.         visited.resize(n, vector<int>(m, -1));
  26.  
  27.         // Инициализация смещений
  28.         dx[0] = -1; dy[0] = 0;
  29.         dx[1] = 1;  dy[1] = 0;
  30.         dx[2] = 0;  dy[2] = -1;
  31.         dx[3] = 0;  dy[3] = 1;
  32.  
  33.         sx = sy = tx = ty = -1;
  34.     }
  35.  
  36.     void read_grid() {
  37.         for (int i = 0; i < n; ++i) {
  38.             string line;
  39.             cin >> line;
  40.             for (int j = 0; j < m; ++j) {
  41.                 grid[i][j] = line[j];
  42.                 if (grid[i][j] == 'S') {
  43.                     sx = i;
  44.                     sy = j;
  45.                 } else if (grid[i][j] == 'T') {
  46.                     tx = i;
  47.                     ty = j;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     bool is_valid(int x, int y) {
  54.         return x >= 0 && x < n && y >= 0 && y < m;
  55.     }
  56.  
  57.     int bfs() {
  58.         if (sx == -1 || tx == -1) {
  59.             return -1;
  60.         }
  61.  
  62.         queue<pair<int, int> > q;
  63.         q.push(make_pair(sx, sy));
  64.         visited[sx][sy] = 0;
  65.  
  66.         while (!q.empty()) {
  67.             pair<int, int> current = q.front();
  68.             q.pop();
  69.             int x = current.first;
  70.             int y = current.second;
  71.             int presses = visited[x][y];
  72.  
  73.             if (x == tx && y == ty) {
  74.                 return presses;
  75.             }
  76.  
  77.             for (int dir = 0; dir < 4; ++dir) {
  78.                 int nx = x;
  79.                 int ny = y;
  80.                 int steps = 0;
  81.  
  82.                 // Движение в направлении dir до стены или препятствия
  83.                 while (true) {
  84.                     int next_x = nx + dx[dir];
  85.                     int next_y = ny + dy[dir];
  86.  
  87.                     if (!is_valid(next_x, next_y) || grid[next_x][next_y] == '#') {
  88.                         break;
  89.                     }
  90.                     nx = next_x;
  91.                     ny = next_y;
  92.                     steps++;
  93.                 }
  94.  
  95.                 if (steps == 0) {
  96.                     continue; // Машинка не может двигаться в этом направлении
  97.                 }
  98.  
  99.                 // Отскок на половину пройденного пути (целочисленное деление)
  100.                 int bounce_steps = steps / 2;
  101.  
  102.                 // Позиция после отскока
  103.                 int final_x = nx - dx[dir] * bounce_steps;
  104.                 int final_y = ny - dy[dir] * bounce_steps;
  105.  
  106.                 if (visited[final_x][final_y] == -1 || visited[final_x][final_y] > presses + 1) {
  107.                     visited[final_x][final_y] = presses + 1;
  108.                     q.push(make_pair(final_x, final_y));
  109.                 }
  110.             }
  111.         }
  112.  
  113.         return -1;
  114.     }
  115. };
  116.  
  117. int main() {
  118.     int n, m;
  119.     cin >> n >> m;
  120.  
  121.     Grid grid(n, m);
  122.     grid.read_grid();
  123.  
  124.     int result = grid.bfs();
  125.  
  126.     cout << result << endl;
  127.  
  128.     return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement