mstoyanov7

minesweeper

Jun 22nd, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cctype>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. std::vector<std::vector<char>> readMatrix(int& n, int& m) {
  8.     std::cin >> n >> m;
  9.     std::vector<std::vector<char>> matrix;
  10.     for (int i = 0; i < n; ++i) {
  11.         std::vector<char> row;
  12.         for (int j = 0; j < m; ++j) {
  13.             char c; std::cin >> c;
  14.             row.push_back(c);
  15.         }
  16.         matrix.push_back(row);
  17.     }
  18.     return matrix;
  19. }
  20.  
  21.  
  22. void covertMinesweeper(std::vector<std::vector<char>>& matrix, std::vector<std::vector<int>>& mineSweeper) {
  23.     for (int i = 0; i < matrix.size(); ++i) {
  24.         std::vector<int> row;
  25.         for (int j = 0; j < matrix[i].size(); ++j) {
  26.             if (matrix[i][j] == '.') {
  27.                 row.push_back(0);
  28.             }
  29.             else {
  30.                 row.push_back(1);
  31.             }
  32.         }
  33.         mineSweeper.push_back(row);
  34.     }
  35. }
  36.  
  37. void handleBombs(std::vector<std::vector<int>>& mineSweeper, int& row, int& col) {
  38.     int rows = mineSweeper.size();
  39.     int cols = mineSweeper[row].size();
  40.  
  41.     // current bomb = mineSweeper[row][col];
  42.  
  43.     bool added = false;
  44.  
  45.     if (row - 1 >= 0) {
  46.         mineSweeper[row - 1][col]++;
  47.         if (col - 1 >= 0) {
  48.             mineSweeper[row - 1][col - 1]++;
  49.             mineSweeper[row][col - 1]++;
  50.             added = true;
  51.         }
  52.         if (col + 1 < cols) {
  53.             mineSweeper[row - 1][col + 1]++;
  54.             mineSweeper[row][col + 1]++;
  55.             added = true;
  56.         }
  57.     }
  58.  
  59.     if (col + 1 < cols && !added) {
  60.         mineSweeper[row][col + 1]++;
  61.     }
  62.     if (col - 1 >= 0 && !added) {
  63.         mineSweeper[row][col - 1]++;
  64.     }
  65.  
  66.     if (row + 1 < rows) {
  67.         mineSweeper[row + 1][col]++;
  68.         if (col - 1 >= 0) {
  69.             mineSweeper[row + 1][col - 1]++;
  70.         }
  71.         if (col + 1 < cols) {
  72.             mineSweeper[row + 1][col + 1]++;
  73.         }
  74.     }
  75. }
  76.  
  77. void findBombs(std::vector<std::vector<int>>& mineSweeper, std::vector<std::string>& bombs) {
  78.     for (int i = 0; i < mineSweeper.size(); ++i) {
  79.         for (int j = 0; j < mineSweeper[i].size(); ++j) {
  80.             if (mineSweeper[i][j] == 1) {
  81.                 std::string bomb;
  82.                 bomb.append(std::to_string(i)).append(" ").append(std::to_string(j));
  83.                 bombs.push_back(bomb);
  84.                 bomb = "";
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. void solution(std::vector<std::vector<int>>& mineSweeper, std::vector<std::string>& bombs) {
  91.     for (auto& line : bombs) {
  92.         std::istringstream istr(line);
  93.  
  94.         int currRowBomb = 0;
  95.         int currColBomb = 0;
  96.  
  97.         istr >> currRowBomb;
  98.         istr >> currColBomb;
  99.  
  100.         handleBombs(mineSweeper, currRowBomb, currColBomb);
  101.     }
  102. }
  103.  
  104. void printMineSweeper(std::vector<std::vector<int>>& mineSweeper) {
  105.     for (auto& row : mineSweeper) {
  106.         for (auto& elem : row) {
  107.             std::cout << elem;
  108.         }
  109.         std::cout << std::endl;
  110.     }
  111. }
  112.  
  113. int main() {
  114.     int n, m;
  115.     std::vector<std::vector<char>> matrix = readMatrix(n, m);
  116.     std::vector<std::vector<int>> mineSweeper;
  117.     covertMinesweeper(matrix, mineSweeper);
  118.     std::vector<std::string> bombs;
  119.     findBombs(mineSweeper, bombs);
  120.  
  121.     solution(mineSweeper, bombs);
  122.  
  123.  
  124.     printMineSweeper(mineSweeper);
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment