Advertisement
Purposelessness

Untitled

Dec 29th, 2022
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <queue>
  5. #include <array>
  6.  
  7. struct Point {
  8.   int x;
  9.   int y;
  10. };
  11.  
  12. int m = 0;
  13. int r = 0;
  14. std::vector<std::vector<int>> map;
  15.  
  16. bool validatePoint(const Point& point, int color) {
  17.   int x = point.x;
  18.   int y = point.y;
  19.   bool flag = false;
  20.   for (int i = 0; i < r; ++i) {
  21.     std::array<int, 4> arr{map[y - i][x], map[y][x - i], map[y - r + 1][x - i], map[y - i][x - r + 1]};
  22.     for (auto& p : arr) {
  23.       if (p == -1) {
  24.         return false;
  25.       }
  26.       if (color < p || p == 0) {
  27.         flag = true;
  28.       }
  29.     }
  30.   }
  31.   if (flag) {
  32.     for (int i = 0; i < r; ++i) {
  33.       for (int j = 0; j < r; ++j) {
  34.         if (map[point.y - i][point.x - j] == 0 || color < map[point.y - i][point.x - j]) {
  35.           map[point.y - i][point.x - j] = color;
  36.         }
  37.       }
  38.     }
  39.   }
  40.   return flag;
  41. }
  42.  
  43. void validatePoints(std::queue<Point>& points, int color) {
  44.   size_t size = points.size();
  45.   for (size_t i = 0; i < size; ++i) {
  46.     auto& point = points.front();
  47.     if (validatePoint(point, color)) {
  48.       points.emplace(point.x + 1, point.y);
  49.       points.emplace(point.x - 1, point.y);
  50.       points.emplace(point.x, point.y - 1);
  51.       points.emplace(point.x, point.y + 1);
  52.     }
  53.     points.pop();
  54.   }
  55. }
  56.  
  57. int main() {
  58.   std::ifstream input("input.txt");
  59.   if (!input.good()) {
  60.     std::cout << "File error: input.txt\n";
  61.     return 1;
  62.   }
  63.   input >> m >> r;
  64.   map.resize(m + 2);
  65.   for (int i = 0; i < m + 2; ++i) {
  66.     map[i].resize(m + 2);
  67.   }
  68.   for (int i = 0; i < m + 2; ++i) {
  69.     map[0][i] = -1;
  70.     map[i][0] = -1;
  71.     map[m + 1][i] = -1;
  72.     map[i][m + 1] = -1;
  73.   }
  74.   for (int i = 1; i < m + 1; ++i) {
  75.     char c = 0;
  76.     for (int j = 1; j < m + 1; ++j) {
  77.       input >> c;
  78.       map[i][j] = c == '.' ? 0 : -1;
  79.     }
  80.   }
  81.   input.close();
  82.   for (int i = 0; i < r; ++i) {
  83.     for (int j = 0; j < r; ++j) {
  84.       map[1 + i][1 + j] = 1;
  85.     }
  86.   }
  87.  
  88.   std::queue<Point> points;
  89.   points.emplace(r + 1, r);
  90.   points.emplace(r - 1, r);
  91.   points.emplace(r, r + 1);
  92.   points.emplace(r, r - 1);
  93.   for (int color = 2;  !points.empty(); ++color) {
  94.     validatePoints(points, color);
  95.   }
  96.  
  97.   for (int i = 0; i < map.size(); ++i) {
  98.     for (int j = 0; j < map.size(); ++j) {
  99.       std::cout << map[i][j] << ' ';
  100.     }
  101.     std::cout << '\n';
  102.   }
  103.  
  104.   std::ofstream output("output.txt");
  105.   if (!output.good()) {
  106.     std::cout << "File error: output.txt\n";
  107.     return 1;
  108.   }
  109.   if (map[m][m] == 0) {
  110.     output << "No";
  111.   } else {
  112.     output << map[m][m] - 1;
  113.   }
  114.  
  115.   return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement