-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path529.cpp
More file actions
45 lines (43 loc) · 1.33 KB
/
529.cpp
File metadata and controls
45 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int row, col;
int dirs[10] = {-1,-1,0,1,1,1,0,-1,-1,-1};
void reveal(vector<vector<char>>& board, int x, int y){
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty()){
auto curr = q.front();q.pop();
int px = curr.first, py = curr.second;
if(board[px][py]!='E')
continue;
int mines = 0;
vector<pair<int,int>> tmp;
for(int i=0; i<8; i++){
int nr = px+dirs[i];
int nc = py+dirs[i+2];
if(0<=nr && 0<=nc && nr<row && nc<col){
if(board[nr][nc]=='M')
mines++;
else if(board[nr][nc]=='E')
tmp.push_back({nr,nc});
}
}
if(!mines){
for(auto p:tmp)
q.push(p);
board[px][py] = 'B';
}
else
board[px][py] = char(mines+48);
}
}
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
row = board.size(), col = board[0].size();
int x = click[0], y = click[1];
if(board[x][y]=='M')
board[x][y]='X';
else
reveal(board, x, y);
return board;
}
};