https://leetcode.com/problems/minesweeper/?tab=Description
计算扫雷游戏点击当前位置后显示结果
BFS,不能一直深搜,因为当前位置如果要显示数字(即周围有几个雷),那么不能对它的四周进行深搜。
因此先count当前四周雷数,如果没有雷才能继续递归搜索
public class Solution {int[][] dirs = {{1, 0}, {1, 1}, {1, -1}, {-1, 0}, {-1, 1}, {-1, -1}, {0, 1}, {0, -1}};public char[][] updateBoard(char[][] board, int[] click) {if (board == null || board.length == 0) {return board;}int i = click[0];int j = click[1];if (board[i][j] == 'M') {board[i][j] = 'X';return board;}update(board, i, j);return board;}private void update(char[][] board, int i, int j) {if (board[i][j] != 'E') {return;}int cnt = 0;for (int[] dir : dirs) {int row = dir[0] + i;int col = dir[1] + j;if (row >= 0 && row < board.length && col >= 0 && col < board[0].length && board[row][col] == 'M') {cnt++;}}board[i][j] = '*';if (cnt == 0) {board[i][j] = 'B';for (int[] dir : dirs) {int row = dir[0] + i;int col = dir[1] + j;if (row >= 0 && row < board.length && col >= 0 && col < board[0].length) {update(board, row, col);}}} else {board[i][j] = (char) (cnt + '0');}}
}
https://leetcode.com/problems/minesweeper/?tab=Description