mitakvd

04. Rust

Oct 12th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. #include<vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<vector<char>> readInput()
  8. {
  9.     vector<vector<char>> matrix;
  10.     const int rows = 10;
  11.     const int cols = 10;
  12.     matrix.resize(rows);
  13.     for (int i = 0; i < rows; i++)
  14.     {
  15.         matrix[i].resize(cols);
  16.         for (int j = 0; j < cols; j++)
  17.         {
  18.             cin >> matrix[i][j];
  19.         }
  20.     }
  21.  
  22.     return matrix;
  23. }
  24.  
  25. void findAllRust(const vector<vector<char>> & matrix,
  26.                   queue<int> & outRows,
  27.                   queue<int> & outCols)
  28. {
  29.     const int rows = matrix.size();
  30.     for (int i = 0; i < rows; i++)
  31.     {
  32.         const int cols = matrix[i].size();
  33.         for (int j = 0; j < cols; j++)
  34.         {
  35.             if (matrix[i][j] == '!')
  36.             {
  37.                 outRows.push(i);
  38.                 outCols.push(j);
  39.             }
  40.         }
  41.     }
  42.  
  43. }
  44.  
  45. void findAllResistantParts(const vector<vector<char>> & matrix,
  46.                             queue<int> & resistRows,
  47.                             queue<int> & resistCols)
  48. {
  49.     const int rows = matrix.size();
  50.     for (int i = 0; i < rows; i++)
  51.     {
  52.         const int cols = matrix[i].size();
  53.         for (int j = 0; j < cols; j++)
  54.         {
  55.             if (matrix[i][j] == '#')
  56.             {
  57.                 resistRows.push(i);
  58.                 resistCols.push(j);
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. bool isInMatrix(vector<vector<char>> & matrix,
  65.                 const int row,
  66.                 const int col)
  67. {
  68.     if (row >= 0 &&
  69.         row < (int)matrix.size() &&
  70.         col >= 0 &&
  71.         col < (int)matrix[row].size())
  72.     {
  73.         return true;
  74.     }
  75.  
  76.     return false;
  77. }
  78.  
  79. bool isResistantPart(vector<vector<char>>& matrix,
  80.     const int row,
  81.     const int col)
  82. {
  83.     if (matrix[row][col] == '#')
  84.     {
  85.         return true;
  86.     }
  87.    
  88.     return false;
  89. }
  90.  
  91. void rustOnes(vector<vector<char>> & matrix,
  92.                         const int row,
  93.                         const int col)
  94. {
  95.     ///top
  96.     if (isInMatrix(matrix, row - 1, col))
  97.     {
  98.         matrix[row - 1][col] = '!';
  99.     }
  100.  
  101.     ///bottom
  102.     if (isInMatrix(matrix, row + 1, col))
  103.     {
  104.         matrix[row + 1][col] = '!';
  105.     }
  106.  
  107.     ///left
  108.     if (isInMatrix(matrix, row, col - 1))
  109.     {
  110.         matrix[row][col - 1] = '!';
  111.     }
  112.  
  113.     ///right
  114.     if (isInMatrix(matrix, row, col + 1))
  115.     {
  116.         matrix[row][col + 1] = '!';
  117.     }
  118.  
  119.     ///center
  120.     if (isInMatrix(matrix, row, col))
  121.     {
  122.         matrix[row][col] = '!';
  123.     }
  124. }
  125.  
  126.  
  127. void rustIsGrowing(vector<vector<char>> & matrix,
  128.                       queue<int> & minesRows,
  129.                       queue<int> & minesCols)
  130. {
  131.     int currRow = 0;
  132.     int currCol = 0;
  133.     while (!minesRows.empty())
  134.     {
  135.         currRow = minesRows.front();
  136.         minesRows.pop();
  137.  
  138.         currCol = minesCols.front();
  139.         minesCols.pop();
  140.  
  141.         rustOnes(matrix, currRow, currCol);
  142.     }
  143. }
  144.  
  145. bool isNewRust (vector<vector<char>> & matrix, const int row, const int col)
  146. {
  147.     if (isInMatrix(matrix, row - 1, col) &&
  148.         !isResistantPart(matrix, row - 1, col) &&
  149.         matrix[row - 1][col] == '.') ///up
  150.     {
  151.         return true;
  152.     }
  153.     else if (isInMatrix(matrix, row + 1, col) &&
  154.              !isResistantPart(matrix, row + 1, col) &&
  155.         matrix[row + 1][col] == '.') ///bottom
  156.     {
  157.         return true;
  158.     }
  159.     else if (isInMatrix(matrix, row, col - 1) &&
  160.              !isResistantPart(matrix, row, col - 1) &&
  161.              matrix[row][col - 1] == '.') ///left
  162.     {
  163.         return true;
  164.     }
  165.     else if (isInMatrix(matrix, row, col + 1) &&
  166.              !isResistantPart(matrix, row, col + 1) &&
  167.              matrix[row][col + 1] == '.') ///right
  168.     {
  169.         return true;
  170.     }
  171.  
  172.     return false;
  173. }
  174.  
  175. void findNewRust(vector<vector<char>>& matrix,
  176.     queue<int>& outRows,
  177.     queue<int>& outCols)
  178. {
  179.     const int rows = matrix.size();
  180.     for (int i = 0; i < rows; i++)
  181.     {
  182.         const int cols = matrix[i].size();
  183.         for (int j = 0; j < cols; j++)
  184.         {
  185.             if (matrix[i][j] == '!')
  186.             {
  187.                 int row = i;
  188.                 int col = j;
  189.                 if (isNewRust(matrix, row, col))
  190.                 {
  191.                     outRows.push(i);
  192.                     outCols.push(j);
  193.                 }
  194.             }
  195.         }
  196.     }
  197. }
  198.  
  199. void printOutput(const vector<vector<char>> & matrix)
  200. {
  201.     for (auto & row : matrix)
  202.     {
  203.         for (auto element : row)
  204.         {
  205.             cout << element;
  206.         }
  207.  
  208.         cout << endl;
  209.     }
  210. }
  211.  
  212. int main()
  213. {
  214.     vector<vector<char>> matrix = readInput();
  215.     ///vector<vector<char>> outputMatrix(matrix.size(), vector<char>(matrix[0].size(), '.'));
  216.  
  217.     int elapsedTime;
  218.     cin >> elapsedTime;
  219.  
  220.     queue<int> minesRows;
  221.     queue<int> minesCols;
  222.  
  223.     findAllRust(matrix, minesRows, minesCols);
  224.  
  225.     queue<int> resistPartRows;
  226.     queue<int> resistPartCols;
  227.  
  228.     findAllResistantParts(matrix, resistPartRows, resistPartCols);
  229.  
  230.     while (elapsedTime > 0)
  231.     {
  232.         rustIsGrowing(matrix, minesRows, minesCols);
  233.         findNewRust(matrix, minesRows, minesCols);
  234.         elapsedTime--;
  235.     }
  236.    
  237.     printOutput(matrix);
  238.  
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment