-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path529.cpp
43 lines (42 loc) · 1.22 KB
/
529.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
// simple.cpp
class Solution {
enum { GAME_OVER, CLICK_SUCCESS };
int height, width, n;
public:
vector<vector<char>> updateBoard(vector<vector<char>> &board,
vector<int> &click) {
height = board.size(), width = board[0].size(), n = click.size();
for (int i = 0; i < n; i += 2)
if (onClick(board, click[i], click[i + 1]) == GAME_OVER)
return board;
return board;
}
int onClick(vector<vector<char>> &board, int x, int y) {
switch (board[x][y]) {
case 'M': {
board[x][y] = 'X';
return GAME_OVER;
}
case 'E': {
int count = 0;
for (int i = -1; i <= 1; ++i)
for (int j = -1; j <= 1; ++j)
if (x + i < 0 || y + j < 0 || x + i >= height || y + j >= width)
continue;
else if (board[x + i][y + j] == 'M')
++count;
if (count == 0) {
board[x][y] = 'B';
for (int i = -1; i <= 1; ++i)
for (int j = -1; j <= 1; ++j)
if (x + i < 0 || y + j < 0 || x + i >= height || y + j >= width)
continue;
else
onClick(board, x + i, y + j);
} else
board[x][y] = '0' + count;
}
}
return CLICK_SUCCESS;
}
};