Advertisement
coloriot

HA_29

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