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