-
Notifications
You must be signed in to change notification settings - Fork 28
/
Minesweeper.cpp
52 lines (48 loc) · 1.44 KB
/
Minesweeper.cpp
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
49
50
51
52
class Solution {
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool valid(int x, int y, vector<vector<char>>& board) {
int n = (int)board.size();
int m = (int)board[0].size();
return x >= 0 and y >= 0 and x < n and y < m;
}
int countMine(int x, int y, vector<vector<char>>& board) {
int cnt = 0;
for(int i = 0; i < 8; i++) {
int X = x + dx[i];
int Y = y + dy[i];
cnt += (valid(X, Y, board) and board[X][Y] == 'M');
}
return cnt;
}
void reveal(int x, int y, vector<vector<char>>& board) {
int mine = countMine(x, y, board);
if(mine > 0) {
board[x][y] = mine + '0';
return;
}
board[x][y] = 'B';
for(int i = 0; i < 8; i++) {
int X = x + dx[i];
int Y = y + dy[i];
if(valid(X, Y, board) and board[X][Y] == 'E') {
reveal(X, Y, board);
}
}
}
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int x = click[0], y = click[1];
switch(board[x][y]) {
case 'M':
board[x][y] = 'X';
break;
case 'E':
reveal(x, y, board);
break;
default:
break;
}
return board;
}
};