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;
- int rows = 0;
- int cols = 0;
- cin >> rows >> cols;
- 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 findAllMines(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);
- }
- }
- }
- }
- bool isInBound(vector<vector<int>> & 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;
- }
- void detonateSingleMine(vector<vector<int>> & outputMatrix,
- const int row,
- const int col)
- {
- ///upper left
- if (isInBound(outputMatrix, row - 1, col - 1))
- {
- outputMatrix[row - 1][col - 1] += 1;
- }
- ///top
- if (isInBound(outputMatrix, row - 1, col))
- {
- outputMatrix[row - 1][col] += 1;
- }
- ///upper right
- if (isInBound(outputMatrix, row - 1, col + 1))
- {
- outputMatrix[row - 1][col + 1] += 1;
- }
- ///right
- if (isInBound(outputMatrix, row, col + 1))
- {
- outputMatrix[row][col + 1] += 1;
- }
- ///lower right
- if (isInBound(outputMatrix, row + 1, col + 1))
- {
- outputMatrix[row + 1][col + 1] += 1;
- }
- ///lower
- if (isInBound(outputMatrix, row + 1, col))
- {
- outputMatrix[row + 1][col] += 1;
- }
- ///lower left
- if (isInBound(outputMatrix, row + 1, col - 1))
- {
- outputMatrix[row + 1][col - 1] += 1;
- }
- ///left
- if (isInBound(outputMatrix, row, col - 1))
- {
- outputMatrix[row][col - 1] += 1;
- }
- ///center
- if (isInBound(outputMatrix, row, col))
- {
- outputMatrix[row][col] += 1;
- }
- }
- void detonateAllMines(vector<vector<int>> & outputMatrix,
- 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();
- detonateSingleMine(outputMatrix, currRow, currCol);
- }
- }
- void printOutput(const vector<vector<int>> & outputMatrix)
- {
- for (auto & row : outputMatrix)
- {
- for (auto element : row)
- {
- cout << element;
- }
- cout << endl;
- }
- }
- int main()
- {
- vector<vector<char>> matrix = readInput();
- vector<vector<int>> outputMatrix(matrix.size(), vector<int>(matrix[0].size(), 0));
- queue<int> minesRows;
- queue<int> minesCols;
- findAllMines(matrix, minesRows, minesCols);
- detonateAllMines(outputMatrix, minesRows, minesCols);
- printOutput(outputMatrix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment