原题目:https://leetcode-cn.com/problems/minesweeper/
思路:
dfs,注意规则就好
代码:
class Solution {
int x[8] = {-1,-1,-1,0,0,1,1,1};
int y[8] = {-1,0,1,-1,1,-1,0,1};
void dfs(vector<vector<char>>& board, int p, int q){
int count=0;
for(int i = 0;i<8;i++){
int tp = p+x[i],tq = q+y[i];
if(tp<0 || tp>=board.size() ||tq<0||tq>=board[0].size()) continue;
if(board[tp][tq] == 'M') ++count;
}
if(count == 0){
board[p][q] = 'B';
for(int i=0;i<8;i++){
int tp = p+x[i],tq = q+y[i];
if(tp<0 || tp>=board.size() ||tq<0||tq>=board[0].size()||board[tp][tq]!='E') continue;
dfs(board,tp,tq);
}
}
else{
board[p][q] = '0'+count;
}
}
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int m = board.size(),n = board[0].size();
int p = click[0],q=click[1];
if(board[p][q] == 'M') board[p][q] = 'X';
else dfs(board,p,q);
return board;
}
};