Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cctype>
- #include <string>
- #include <sstream>
- std::vector<std::vector<char>> readMatrix(int& n, int& m) {
- std::cin >> n >> m;
- std::vector<std::vector<char>> matrix;
- for (int i = 0; i < n; ++i) {
- std::vector<char> row;
- for (int j = 0; j < m; ++j) {
- char c; std::cin >> c;
- row.push_back(c);
- }
- matrix.push_back(row);
- }
- return matrix;
- }
- void covertMinesweeper(std::vector<std::vector<char>>& matrix, std::vector<std::vector<int>>& mineSweeper) {
- for (int i = 0; i < matrix.size(); ++i) {
- std::vector<int> row;
- for (int j = 0; j < matrix[i].size(); ++j) {
- if (matrix[i][j] == '.') {
- row.push_back(0);
- }
- else {
- row.push_back(1);
- }
- }
- mineSweeper.push_back(row);
- }
- }
- void handleBombs(std::vector<std::vector<int>>& mineSweeper, int& row, int& col) {
- int rows = mineSweeper.size();
- int cols = mineSweeper[row].size();
- // current bomb = mineSweeper[row][col];
- bool added = false;
- if (row - 1 >= 0) {
- mineSweeper[row - 1][col]++;
- if (col - 1 >= 0) {
- mineSweeper[row - 1][col - 1]++;
- mineSweeper[row][col - 1]++;
- added = true;
- }
- if (col + 1 < cols) {
- mineSweeper[row - 1][col + 1]++;
- mineSweeper[row][col + 1]++;
- added = true;
- }
- }
- if (col + 1 < cols && !added) {
- mineSweeper[row][col + 1]++;
- }
- if (col - 1 >= 0 && !added) {
- mineSweeper[row][col - 1]++;
- }
- if (row + 1 < rows) {
- mineSweeper[row + 1][col]++;
- if (col - 1 >= 0) {
- mineSweeper[row + 1][col - 1]++;
- }
- if (col + 1 < cols) {
- mineSweeper[row + 1][col + 1]++;
- }
- }
- }
- void findBombs(std::vector<std::vector<int>>& mineSweeper, std::vector<std::string>& bombs) {
- for (int i = 0; i < mineSweeper.size(); ++i) {
- for (int j = 0; j < mineSweeper[i].size(); ++j) {
- if (mineSweeper[i][j] == 1) {
- std::string bomb;
- bomb.append(std::to_string(i)).append(" ").append(std::to_string(j));
- bombs.push_back(bomb);
- bomb = "";
- }
- }
- }
- }
- void solution(std::vector<std::vector<int>>& mineSweeper, std::vector<std::string>& bombs) {
- for (auto& line : bombs) {
- std::istringstream istr(line);
- int currRowBomb = 0;
- int currColBomb = 0;
- istr >> currRowBomb;
- istr >> currColBomb;
- handleBombs(mineSweeper, currRowBomb, currColBomb);
- }
- }
- void printMineSweeper(std::vector<std::vector<int>>& mineSweeper) {
- for (auto& row : mineSweeper) {
- for (auto& elem : row) {
- std::cout << elem;
- }
- std::cout << std::endl;
- }
- }
- int main() {
- int n, m;
- std::vector<std::vector<char>> matrix = readMatrix(n, m);
- std::vector<std::vector<int>> mineSweeper;
- covertMinesweeper(matrix, mineSweeper);
- std::vector<std::string> bombs;
- findBombs(mineSweeper, bombs);
- solution(mineSweeper, bombs);
- printMineSweeper(mineSweeper);
- }
Advertisement
Add Comment
Please, Sign In to add comment