Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- #include<vector>
- using namespace std;
- vector<vector<char>> readInput()
- {
- vector<vector<char>> matrix;
- const int rows = 10;
- const int cols = 10;
- matrix.resize(rows);
- for (int i = 0; i < rows; i++)
- {
- matrix[i].resize(cols);
- for (int j = 0; j < cols; j++)
- {
- cin >> matrix[i][j];
- }
- }
- return matrix;
- }
- void findAllRust(const vector<vector<char>> & matrix,
- queue<int> & outRows,
- queue<int> & outCols)
- {
- const int rows = matrix.size();
- for (int i = 0; i < rows; i++)
- {
- const int cols = matrix[i].size();
- for (int j = 0; j < cols; j++)
- {
- if (matrix[i][j] == '!')
- {
- outRows.push(i);
- outCols.push(j);
- }
- }
- }
- }
- void findAllResistantParts(const vector<vector<char>> & matrix,
- queue<int> & resistRows,
- queue<int> & resistCols)
- {
- const int rows = matrix.size();
- for (int i = 0; i < rows; i++)
- {
- const int cols = matrix[i].size();
- for (int j = 0; j < cols; j++)
- {
- if (matrix[i][j] == '#')
- {
- resistRows.push(i);
- resistCols.push(j);
- }
- }
- }
- }
- bool isInMatrix(vector<vector<char>> & matrix,
- const int row,
- const int col)
- {
- if (row >= 0 &&
- row < (int)matrix.size() &&
- col >= 0 &&
- col < (int)matrix[row].size())
- {
- return true;
- }
- return false;
- }
- bool isResistantPart(vector<vector<char>>& matrix,
- const int row,
- const int col)
- {
- if (matrix[row][col] == '#')
- {
- return true;
- }
- return false;
- }
- void rustOnes(vector<vector<char>> & matrix,
- const int row,
- const int col)
- {
- ///top
- if (isInMatrix(matrix, row - 1, col))
- {
- matrix[row - 1][col] = '!';
- }
- ///bottom
- if (isInMatrix(matrix, row + 1, col))
- {
- matrix[row + 1][col] = '!';
- }
- ///left
- if (isInMatrix(matrix, row, col - 1))
- {
- matrix[row][col - 1] = '!';
- }
- ///right
- if (isInMatrix(matrix, row, col + 1))
- {
- matrix[row][col + 1] = '!';
- }
- ///center
- if (isInMatrix(matrix, row, col))
- {
- matrix[row][col] = '!';
- }
- }
- void rustIsGrowing(vector<vector<char>> & matrix,
- queue<int> & minesRows,
- queue<int> & minesCols)
- {
- int currRow = 0;
- int currCol = 0;
- while (!minesRows.empty())
- {
- currRow = minesRows.front();
- minesRows.pop();
- currCol = minesCols.front();
- minesCols.pop();
- rustOnes(matrix, currRow, currCol);
- }
- }
- bool isNewRust (vector<vector<char>> & matrix, const int row, const int col)
- {
- if (isInMatrix(matrix, row - 1, col) &&
- !isResistantPart(matrix, row - 1, col) &&
- matrix[row - 1][col] == '.') ///up
- {
- return true;
- }
- else if (isInMatrix(matrix, row + 1, col) &&
- !isResistantPart(matrix, row + 1, col) &&
- matrix[row + 1][col] == '.') ///bottom
- {
- return true;
- }
- else if (isInMatrix(matrix, row, col - 1) &&
- !isResistantPart(matrix, row, col - 1) &&
- matrix[row][col - 1] == '.') ///left
- {
- return true;
- }
- else if (isInMatrix(matrix, row, col + 1) &&
- !isResistantPart(matrix, row, col + 1) &&
- matrix[row][col + 1] == '.') ///right
- {
- return true;
- }
- return false;
- }
- void findNewRust(vector<vector<char>>& matrix,
- queue<int>& outRows,
- queue<int>& outCols)
- {
- const int rows = matrix.size();
- for (int i = 0; i < rows; i++)
- {
- const int cols = matrix[i].size();
- for (int j = 0; j < cols; j++)
- {
- if (matrix[i][j] == '!')
- {
- int row = i;
- int col = j;
- if (isNewRust(matrix, row, col))
- {
- outRows.push(i);
- outCols.push(j);
- }
- }
- }
- }
- }
- void printOutput(const vector<vector<char>> & matrix)
- {
- for (auto & row : matrix)
- {
- for (auto element : row)
- {
- cout << element;
- }
- cout << endl;
- }
- }
- int main()
- {
- vector<vector<char>> matrix = readInput();
- ///vector<vector<char>> outputMatrix(matrix.size(), vector<char>(matrix[0].size(), '.'));
- int elapsedTime;
- cin >> elapsedTime;
- queue<int> minesRows;
- queue<int> minesCols;
- findAllRust(matrix, minesRows, minesCols);
- queue<int> resistPartRows;
- queue<int> resistPartCols;
- findAllResistantParts(matrix, resistPartRows, resistPartCols);
- while (elapsedTime > 0)
- {
- rustIsGrowing(matrix, minesRows, minesCols);
- findNewRust(matrix, minesRows, minesCols);
- elapsedTime--;
- }
- printOutput(matrix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment