# Java实现 LeetCode 529 扫雷游戏(DFS)

### 529. 扫雷游戏

[[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘M’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’]]

Click : [3,0]

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Click : [1,2]

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘X’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

PS:

class Solution {
static public char[][] updateBoard(char[][] board, int[] click) {

return visit(board,click[0],click[1]);
}

static public char[][] visit(char[][] board, int x,int y){
if(board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}
int count = 0;
for(int i =x-1;i<=x+1;i++)
for(int j =y-1; j<= y+1;j++){
if(i>=0 && i < board.length && j>=0 && j<board[0].length)
if(board[i][j] == 'M')
count++;
}
if(count != 0 ) {
board[x][y] = (char)(count+'0');
}
else {
board[x][y] = 'B';
for(int i =x-1;i<=x+1;i++)
for(int j =y-1; j<= y+1;j++){
if(i>=0 && i < board.length && j>=0 && j<board[0].length)
if(board[i][j] == 'E')
visit(board,i,j);
}

}

return board;
}
}


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
10-22 4841
12-21 1万+
05-14 562
03-01 212
10-24 3万+
02-12 1432
08-05 817
03-05 779
11-09 720

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

weixin_45176257

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。