Advertisement
coloriot

HA_28

Oct 25th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. class Graph {
  10. private:
  11.     vector<vector<int> > adj;
  12.     vector<bool> used;
  13.  
  14. public:
  15.     Graph(int vertex_count) {
  16.         adj.resize(vertex_count);
  17.         used.resize(vertex_count, false);
  18.     }
  19.  
  20.     void add_edge(int from, int to) {
  21.         adj[from].push_back(to);
  22.     }
  23.  
  24.     bool BFS(int start_v, int end_v) {
  25.         queue<int> q;
  26.         q.push(start_v);
  27.         used[start_v] = true;
  28.         while (!q.empty()) {
  29.             int cur_v = q.front();
  30.             q.pop();
  31.             if (cur_v == end_v) {
  32.                 return true;
  33.             }
  34.             for (size_t i = 0; i < adj[cur_v].size(); ++i) {
  35.                 int nei = adj[cur_v][i];
  36.                 if (!used[nei]) {
  37.                     q.push(nei);
  38.                     used[nei] = true;
  39.                 }
  40.             }
  41.         }
  42.         return false;
  43.     }
  44. };
  45.  
  46. class Grid {
  47. private:
  48.     vector<vector<int> > matrix;
  49.     int rows;
  50.     int cols;
  51.     Graph* graph;
  52.  
  53.     int index(int x, int y) const {
  54.         return x * cols + y;
  55.     }
  56.  
  57.     bool is_valid_cell(int x, int y) const {
  58.         return x >= 0 && x < rows && y >= 0 && y < cols;
  59.     }
  60.  
  61. public:
  62.     Grid(const vector<vector<int> >& matrix) : matrix(matrix), graph(NULL) {
  63.         rows = matrix.size();
  64.         cols = matrix.empty() ? 0 : matrix[0].size();
  65.     }
  66.  
  67.     ~Grid() {
  68.         delete graph;
  69.     }
  70.  
  71.     void build_graph() {
  72.         int vertex_count = rows * cols;
  73.         graph = new Graph(vertex_count);
  74.  
  75.         int dx[] = { -1, 1, 0, 0 };
  76.         int dy[] = { 0, 0, -1, 1 };
  77.  
  78.         for (int i = 0; i < rows; ++i) {
  79.             for (int j = 0; j < cols; ++j) {
  80.                 if (matrix[i][j] == 1) {
  81.                     int from = index(i, j);
  82.                     for (int dir = 0; dir < 4; ++dir) {
  83.                         int ni = i + dx[dir];
  84.                         int nj = j + dy[dir];
  85.                         if (is_valid_cell(ni, nj) && matrix[ni][nj] == 1) {
  86.                             int to = index(ni, nj);
  87.                             graph->add_edge(from, to);
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
  94.     bool find_path(int start_x, int start_y, int end_x, int end_y) {
  95.         bool start_valid = is_valid_cell(start_x, start_y);
  96.         bool end_valid = is_valid_cell(end_x, end_y);
  97.  
  98.         if (!start_valid || !end_valid) {
  99.             return false;
  100.         }
  101.         bool start_accessible = (matrix[start_x][start_y] == 1);
  102.         bool end_accessible = (matrix[end_x][end_y] == 1);
  103.  
  104.         if (!start_accessible || !end_accessible) {
  105.             return false;
  106.         }
  107.  
  108.         int start_vertex = index(start_x, start_y);
  109.         int end_vertex = index(end_x, end_y);
  110.  
  111.         return graph->BFS(start_vertex, end_vertex);
  112.     }
  113. };
  114.  
  115. int main() {
  116.     vector<vector<int> > matrix;
  117.     vector<int> coordinates;
  118.     string line;
  119.  
  120.     while (getline(cin, line)) {
  121.         if (line.empty()) {
  122.             continue;
  123.         }
  124.  
  125.         vector<int> nums;
  126.         istringstream iss(line);
  127.         int num;
  128.         while (iss >> num) {
  129.             nums.push_back(num);
  130.         }
  131.  
  132.         if (nums.size() == 2) {
  133.             coordinates.insert(coordinates.end(), nums.begin(), nums.end());
  134.             if (coordinates.size() == 4) {
  135.                 break;
  136.             }
  137.         } else if (!nums.empty()) {
  138.             matrix.push_back(nums);
  139.         }
  140.     }
  141.  
  142.     if (coordinates.size() != 4 || matrix.empty()) {
  143.         cout << "Некорректный ввод" << endl;
  144.         return 0;
  145.     }
  146.  
  147.     int start_x = coordinates[0] - 1;
  148.     int start_y = coordinates[1] - 1;
  149.     int end_x = coordinates[2] - 1;
  150.     int end_y = coordinates[3] - 1;
  151.  
  152.     Grid grid(matrix);
  153.     grid.build_graph();
  154.  
  155.     if (grid.find_path(start_x, start_y, end_x, end_y)) {
  156.         cout << "Дорога есть" << endl;
  157.     } else {
  158.         cout << "Дороги нет" << endl;
  159.     }
  160.  
  161.     return 0;
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement