-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0529_updateBoard.cc
67 lines (63 loc) · 1.33 KB
/
Problem_0529_updateBoard.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <vector>
using namespace std;
class Solution
{
public:
void f(vector<vector<char>>& board, int x, int y)
{
if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size())
{
// 越界或者已经是揭开空白块
return;
}
if (board[x][y] == 'M')
{
// 碰到地雷
board[x][y] = 'X';
return;
}
if (board[x][y] != 'E')
{
// 已经揭开不需要扩展
return;
}
int mine = 0;
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
int nextx = x + dx;
int nexty = y + dy;
if (nextx < 0 || nextx >= board.size() || nexty < 0 || nexty >= board[0].size() ||
(nextx == x && nexty == y))
{
continue;
}
mine += board[nextx][nexty] == 'M';
}
}
if (mine == 0)
{
board[x][y] = 'B';
f(board, x - 1, y - 1);
f(board, x - 1, y);
f(board, x - 1, y + 1);
f(board, x, y - 1);
f(board, x, y + 1);
f(board, x + 1, y - 1);
f(board, x + 1, y);
f(board, x + 1, y + 1);
}
else
{
board[x][y] = mine + '0';
}
}
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
int x = click[0];
int y = click[1];
f(board, x, y);
return board;
}
};