mstoyanov7

rust

Jun 26th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <queue>
  4.  
  5. const int size = 10;
  6.  
  7. std::array<std::array<char, size>, size> readInput() {
  8.     std::array<std::array<char, size>, size> input;
  9.     for (auto& row : input) {
  10.         for (auto& elem : row) {
  11.             std::cin >> elem;
  12.         }
  13.     }
  14.     return input;
  15. }
  16.  
  17. void findRustPoints(std::array<std::array<char, size>, size>& input, std::queue<std::pair<int, int>>& currentRust) {
  18.     for (int i = 0; i < size; ++i) {
  19.         for (int j = 0; j < size; ++j) {
  20.             if (input[i][j] == '!') {
  21.                 currentRust.push(std::make_pair(i, j));
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. void makeOneMove(std::array<std::array<char, size>, size>& input, std::queue<std::pair<int, int>>& currentRust) {
  28.     while (!currentRust.empty()) {
  29.  
  30.         std::pair<int, int> currentRustPair = currentRust.front();
  31.         int row = currentRustPair.first;
  32.         int col = currentRustPair.second;
  33.  
  34.         if (row - 1 >= 0) {
  35.             if (input[row - 1][col] != '#') {
  36.                 input[row - 1][col] = '!';
  37.             }
  38.         }
  39.         if (row + 1 < size) {
  40.             if (input[row + 1][col] != '#') {
  41.                 input[row + 1][col] = '!';
  42.             }
  43.         }
  44.  
  45.         if (col - 1 >= 0) {
  46.             if (input[row][col - 1] != '#') {
  47.                 input[row][col - 1] = '!';
  48.             }
  49.         }
  50.  
  51.         if (col + 1 < size) {
  52.             if (input[row][col + 1] != '#') {
  53.                 input[row][col + 1] = '!';
  54.             }
  55.         }
  56.         currentRust.pop();
  57.     }
  58. }
  59.  
  60. void processRusting(std::array<std::array<char, size>, size>& input, std::queue<std::pair<int, int>>& currentRust, int& rustCount) {
  61.     for (int i = 0; i < rustCount; ++i) {
  62.         findRustPoints(input, currentRust);
  63.         makeOneMove(input, currentRust);
  64.     }
  65. }
  66.  
  67. void printSolution(std::array<std::array<char, size>, size>& input) {
  68.     for (auto& row : input) {
  69.         for (auto& elem : row) {
  70.             std::cout << elem;
  71.         }
  72.         std::cout << std::endl;
  73.     }
  74. }
  75.  
  76. int main() {
  77.     std::array<std::array<char, size>, size> input = readInput();
  78.     int rustCount = 0;
  79.     std::cin >> rustCount;
  80.     std::queue<std::pair<int, int>> currentRust;
  81.  
  82.     processRusting(input, currentRust, rustCount);
  83.     printSolution(input);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment