-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMine_Sweeper.cpp
More file actions
48 lines (43 loc) · 1.36 KB
/
Mine_Sweeper.cpp
File metadata and controls
48 lines (43 loc) · 1.36 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
46
47
48
class Solution {
public:
int row, col;
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};
bool isok(int x, int y) { return x>=0 && x<row && y>=0 && y<col; }
void reveal(vector<vector<char>>& board, int x, int y)
{
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty())
{
pair<int,int> p = q.front();
q.pop();
int px = p.first, py = p.second;
if(board[px][py]!='E') continue;
int mines = 0;
vector<pair<int,int>> tmp;
for(int i=0; i<8; i++)
{
if(isok(px+dx[i], py+dy[i]))
{
if(board[px+dx[i]][py+dy[i]]=='M') mines++;
else if(board[px+dx[i]][py+dy[i]]=='E') tmp.push_back({px+dx[i], py+dy[i]});
}
}
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;
}
};