mstoyanov7

icozada4a

Oct 19th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. std::vector<std::vector<int>> readSize() {
  6.     std::vector<std::vector<int>> vec;
  7.     int x, y;
  8.     std::cin >> x >> y;
  9.    
  10.     for (int i = 0; i < x; ++i) {
  11.         std::vector<int> row(y);
  12.         vec.push_back(row);
  13.     }
  14.     return vec;
  15. }
  16.  
  17. std::pair<int, int> readStartingCoord() {
  18.     int x, y;
  19.     std::cin >> x >> y;
  20.     return std::make_pair(x, y);
  21. }
  22.  
  23. void makeMove(std::vector<std::vector<int>>& field, int& x, int& y, int& target) {
  24.     if (x - 1 >= 0) {
  25.         if (field[x - 1][y] == 0) field[x - 1][y] = target;
  26.         if (y - 1 >= 0 && field[x - 1][y - 1] == 0) field[x - 1][y - 1] = target;
  27.         if (y + 1 < field[x].size() && field[x - 1][y + 1] == 0) field[x - 1][y + 1] = target;
  28.     }
  29.  
  30.     if (x + 1 < field.size()) {
  31.         if (field[x + 1][y] == 0) field[x + 1][y] = target;
  32.         if (y - 1 >= 0 && field[x + 1][y - 1] == 0) field[x + 1][y - 1] = target;
  33.         if (y + 1 < field[x].size() && field[x + 1][y + 1] == 0) field[x + 1][y + 1] = target;
  34.     }
  35.  
  36.     if (y - 1 >= 0) {
  37.         if (field[x][y - 1] == 0) field[x][y - 1] = target;
  38.     }
  39.  
  40.     if (y + 1 < field[x].size()) {
  41.         if (field[x][y + 1] == 0) field[x][y + 1] = target;
  42.     }
  43. }
  44.  
  45. void processSolution(std::vector<std::vector<int>>& field, std::pair<int, int>& coords) {
  46.     field[coords.first][coords.second] = 1;
  47.     int target = 2;
  48.    
  49.     for (int k = 0; k < field.size(); ++k) {
  50.         while (std::find(field[k].begin(), field[k].end(), 0) != field[k].end()) {
  51.             for (int i = 0; i < field.size(); ++i) {
  52.                 for (int j = 0; j < field[i].size(); ++j) {
  53.                     if (field[i][j] == target - 1) {
  54.                         makeMove(field, i, j, target);
  55.                     }
  56.                 }
  57.             }
  58.             target++;
  59.         }
  60.     }
  61. }
  62.  
  63. void printSolution(std::vector<std::vector<int>>& field) {
  64.     for (auto& x : field) {
  65.         for (auto& y : x) {
  66.             std::cout << y << ' ';
  67.         }
  68.         std::cout << std::endl;
  69.     }
  70. }
  71.  
  72. int main() {
  73.     auto field = readSize();
  74.     auto startingCoords = readStartingCoord();
  75.     processSolution(field, startingCoords);
  76.     printSolution(field);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment