Advertisement
Purposelessness

Untitled

Dec 26th, 2022
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. int m = 0;
  6. int r = 0;
  7.  
  8. bool tryMoveRight(std::vector<std::vector<int>>& map, int x, int y, int color) {
  9.   for (int i = 0; i < r; ++i) {
  10.     if (map[y - i][x] == -1) {
  11.       return false;
  12.     }
  13.   }
  14.   for (int i = 0; i < r; ++i) {
  15.     map[y - i][x] = color;
  16.   }
  17.   return true;
  18. }
  19.  
  20. bool tryMoveDown(std::vector<std::vector<int>>& map, int x, int y, int color) {
  21.   for (int i = 0; i < r; ++i) {
  22.     if (map[y][x - i] == -1) {
  23.       return false;
  24.     }
  25.   }
  26.   for (int i = 0; i < r; ++i) {
  27.     map[y][x - i] = color;
  28.   }
  29.   return true;
  30. }
  31.  
  32. void findPath(std::vector<std::vector<int>>& map, int x, int y, int color) {
  33.   if (tryMoveRight(map, x + 1, y, color + 1)) {
  34.     findPath(map, x + 1, y, color + 1);
  35.   }
  36.   if (tryMoveDown(map, x, y + 1, color + 1)) {
  37.     findPath(map, x, y + 1, color + 1);
  38.   }
  39. }
  40.  
  41. int main() {
  42.   std::ifstream input("input.txt");
  43.   if (!input.good()) {
  44.     std::cout << "File error: input.txt\n";
  45.     return 1;
  46.   }
  47.   input >> m >> r;
  48.   std::vector<std::vector<int>> map(m + 2);
  49.   for (int i = 0; i < m + 2; ++i) {
  50.     map[i].resize(m + 2);
  51.   }
  52.   for (int i = 0; i < m + 2; ++i) {
  53.     map[0][i] = -1;
  54.     map[i][0] = -1;
  55.     map[m + 1][i] = -1;
  56.     map[i][m + 1] = -1;
  57.   }
  58.   for (int i = 1; i < m + 1; ++i) {
  59.     char c = 0;
  60.     for (int j = 1; j < m + 1; ++j) {
  61.       input >> c;
  62.       map[i][j] = c == '.' ? 0 : -1;
  63.     }
  64.   }
  65.   input.close();
  66.  
  67.   findPath(map, r, r, 0);
  68.  
  69.   std::ofstream output("output.txt");
  70.   if (!output.good()) {
  71.     std::cout << "File error: output.txt\n";
  72.     return 1;
  73.   }
  74.   if (map[m][m] == 0) {
  75.     output << "No";
  76.   } else {
  77.     output << map[m][m];
  78.   }
  79.  
  80.   return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement