题目
具体代码
class Solution {
int[] dx = {-1,-1,-1,0,0,1,1,1};
int[] dy = {-1,0,1,-1,1,-1,0,1};
public char[][] updateBoard(char[][] board, int[] click) {
dfs(board,click[0],click[1]);
return board;
}
public void dfs(char[][] board, int x,int y){
if(x < 0 || x >= board.length || y < 0 || y >= board[0].length){
return;
}
if(board[x][y] == 'E'){
//判断相邻八个方向上是否有雷
int count = judge(board,x,y);
if(count == 0){
//没有相邻地雷,修改为B
board[x][y] = 'B';
//递归遍历相邻方块的雷数
for(int i = 0;i < 8;i++){
dfs(board,x+dx[i],y+dy[i]);
}
}else{
board[x][y] = (char)(count+'0');
}
}
if(board[x][y] == 'M'){
board[x][y] = 'X';
}
}
public int judge(char[][] board,int x,int y){
int count = 0;
for(int i = 0;i < 8;i++){
int newX = x + dx[i], newY = y + dy[i];
if(newX < 0 || newX >= board.length || newY < 0 || newY >= board[0].length){
continue;
}
if(board[x+dx[i]][y+dy[i]]=='M'){
count++;
}
}
return count;
}
}