leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0529.cpp (1251B)


      1 class Solution {
      2   public:
      3     vector<vector<char>> updateBoard(vector<vector<char>> &board, const vector<int> &click) {
      4         const int n = board.size(), m = board[0].size();
      5 
      6         if (board[click[0]][click[1]] == 'M') {
      7             board[click[0]][click[1]] = 'X';
      8             return board;
      9         }
     10 
     11         if (board[click[0]][click[1]] != 'E') return board;
     12 
     13         queue<pair<int, int>> q({{click[0], click[1]}});
     14         while (!q.empty()) {
     15             const auto [x, y] = q.front();
     16             q.pop();
     17             if (board[x][y] != 'E') continue;
     18 
     19             int count = 0;
     20             for (int i = max(x - 1, 0); i <= min(n - 1, x + 1); i++) {
     21                 for (int j = max(y - 1, 0); j <= min(m - 1, y + 1); j++) {
     22                     if (board[i][j] == 'M') count++;
     23                 }
     24             }
     25 
     26             if (count != 0) {
     27                 board[x][y] = '0' + count;
     28                 continue;
     29             }
     30 
     31             board[x][y] = 'B';
     32             for (int i = max(x - 1, 0); i <= min(n - 1, x + 1); i++) {
     33                 for (int j = max(y - 1, 0); j <= min(m - 1, y + 1); j++) {
     34                     if (board[i][j] == 'E') q.push({i, j});
     35                 }
     36             }
     37         }
     38 
     39         return board;
     40     }
     41 };