mitakvd

03. Minesweeper

Oct 12th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 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.  
  11.     int rows = 0;
  12.     int cols = 0;
  13.     cin >> rows >> cols;
  14.     matrix.resize(rows);
  15.     for (int i = 0; i < rows; ++i)
  16.     {
  17.         matrix[i].resize(cols);
  18.         for (int j = 0; j < cols; ++j)
  19.         {
  20.             cin >> matrix[i][j];
  21.         }
  22.     }
  23.  
  24.     return matrix;
  25. }
  26. void findAllMines(const vector<vector<char>> & matrix,
  27.     queue<int> & outRows,
  28.     queue<int> & outCols)
  29. {
  30.     const int rows = matrix.size();
  31.     for (int i = 0; i < rows; ++i)
  32.     {
  33.         const int cols = matrix[i].size();
  34.         for (int j = 0; j < cols; ++j)
  35.         {
  36.             if (matrix[i][j] == '!')
  37.             {
  38.                 outRows.push(i);
  39.                 outCols.push(j);
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45. bool isInBound(vector<vector<int>> & matrix,
  46.     const int row,
  47.     const int col)
  48. {
  49.     if (row >= 0 &&
  50.         row < (int)matrix.size() &&
  51.         col >= 0 &&
  52.         col < (int)matrix[row].size())
  53.     {
  54.         return true;
  55.     }
  56.  
  57.     return false;
  58. }
  59.  
  60. void detonateSingleMine(vector<vector<int>> & outputMatrix,
  61.     const int row,
  62.     const int col)
  63. {
  64.     ///upper left
  65.     if (isInBound(outputMatrix, row - 1, col - 1))
  66.     {
  67.         outputMatrix[row - 1][col - 1] += 1;
  68.     }
  69.  
  70.     ///top
  71.     if (isInBound(outputMatrix, row - 1, col))
  72.     {
  73.         outputMatrix[row - 1][col] += 1;
  74.     }
  75.  
  76.     ///upper right
  77.     if (isInBound(outputMatrix, row - 1, col + 1))
  78.     {
  79.         outputMatrix[row - 1][col + 1] += 1;
  80.     }
  81.  
  82.     ///right
  83.     if (isInBound(outputMatrix, row, col + 1))
  84.     {
  85.         outputMatrix[row][col + 1] += 1;
  86.     }
  87.  
  88.     ///lower right
  89.     if (isInBound(outputMatrix, row + 1, col + 1))
  90.     {
  91.         outputMatrix[row + 1][col + 1] += 1;
  92.     }
  93.  
  94.     ///lower
  95.     if (isInBound(outputMatrix, row + 1, col))
  96.     {
  97.         outputMatrix[row + 1][col] += 1;
  98.     }
  99.  
  100.     ///lower left
  101.     if (isInBound(outputMatrix, row + 1, col - 1))
  102.     {
  103.         outputMatrix[row + 1][col - 1] += 1;
  104.     }
  105.  
  106.     ///left
  107.     if (isInBound(outputMatrix, row, col - 1))
  108.     {
  109.         outputMatrix[row][col - 1] += 1;
  110.     }
  111.  
  112.     ///center
  113.     if (isInBound(outputMatrix, row, col))
  114.     {
  115.         outputMatrix[row][col] += 1;
  116.     }
  117. }
  118.  
  119. void detonateAllMines(vector<vector<int>> & outputMatrix,
  120.     queue<int> & minesRows,
  121.     queue<int> & minesCols)
  122. {
  123.     int currRow = 0;
  124.     int currCol = 0;
  125.     while (!minesRows.empty())
  126.     {
  127.         currRow = minesRows.front();
  128.         minesRows.pop();
  129.  
  130.         currCol = minesCols.front();
  131.         minesCols.pop();
  132.  
  133.         detonateSingleMine(outputMatrix, currRow, currCol);
  134.     }
  135. }
  136.  
  137. void printOutput(const vector<vector<int>> & outputMatrix)
  138. {
  139.     for (auto & row : outputMatrix)
  140.     {
  141.         for (auto element : row)
  142.         {
  143.             cout << element;
  144.         }
  145.  
  146.         cout << endl;
  147.     }
  148. }
  149.  
  150. int main()
  151. {
  152.     vector<vector<char>> matrix = readInput();
  153.     vector<vector<int>> outputMatrix(matrix.size(), vector<int>(matrix[0].size(), 0));
  154.  
  155.     queue<int> minesRows;
  156.     queue<int> minesCols;
  157.  
  158.     findAllMines(matrix, minesRows, minesCols);
  159.     detonateAllMines(outputMatrix, minesRows, minesCols);
  160.     printOutput(outputMatrix);
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment