class Solution {
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int x = click[0];
int y = click[1];
int high = board.size();
int len = board[0].size();
vector<vector<char>> ans(board);
int num = mineNum(board, x, y, len, high);
if (board[x][y] == 'M')
board[x][y] = 'X';
else if (board[x][y] == 'E'&& num)
board[x][y] = num + '0';
else {
dfs(board, x, y, len, high);
}
return board;
}
void dfs(vector<vector<char>>&board, int x, int y, int len, int high) {
board[x][y] = 'B';
int dx, dy;
for (int i = 0; i < 8; i++) {
dx = x + move[i][0];
dy = y + move[i][1];
if (dx >= 0 && dx < high&&dy >= 0 && dy < len&&board[dx][dy] != 'B')
if (!mineNum(board, dx, dy, len, high))
dfs(board, dx, dy, len, high);
else if (board[dx][dy] != 'M')
board[dx][dy] = mineNum(board, dx, dy, len, high) + '0';
}
}
int mineNum(vector<vector<char>>&board, int x, int y, int len, int high) {
int cur = 0;
int dx, dy;
for (int i = 0; i < 8; i++) {
dx = x + move[i][0];
dy = y + move[i][1];
if (dx >= 0 && dx < high&&dy >= 0 && dy < len&&board[dx][dy] == 'M')
cur++;
}
return cur;
}
private:
int move[9][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 },{ -1,-1 },{ -1,1 },{ 1,-1 },{ 1,1 } };
};
Leetcode 529. Minesweeper
最新推荐文章于 2020-09-24 22:58:54 发布