ID | Title | Difficulty | |
---|---|---|---|
Loading... |
529. Minesweeper
Medium
LeetCode
Array, Depth-First Search, Breadth-First Search, Matrix
Problem
Let’s play the minesweeper game (Wikipedia, online game)!
You are given an m x n char matrix board representing the game board where:
- ‘M’ represents an unrevealed mine,
- ‘E’ represents an unrevealed empty square,
- ‘B’ represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
- digit (‘1’ to ‘8’) represents how many mines are adjacent to this revealed square, and
- ‘X’ represents a revealed mine.
You are also given an integer array click where click = [click_r, click_c]
represents the next click position among all the unrevealed squares (‘M’ or ‘E’).
Return the board after revealing this position according to the following rules:
- If a mine ‘M’ is revealed, then the game is over. You should change it to ‘X’.
- If an empty square ‘E’ with no adjacent mines is revealed, then change it to a revealed blank ‘B’ and all of its adjacent unrevealed squares should be revealed recursively.
- If an empty square ‘E’ with at least one adjacent mine is revealed, then change it to a digit (‘1’ to ‘8’) representing the number of adjacent mines.
- Return the board when no more squares will be revealed.
Example 1:
Input: board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
Example 2:
Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
Constraints:
- m == board.length
- n == board[i].length
- 1 <= m, n <= 50
- board[i][j] is either ‘M’, ‘E’, ‘B’, or a digit from ‘1’ to ‘8’.
- click.length == 2
- 0 <= click_r < m
- 0 <= click_c < n
board[click_r][click_c]
is either ‘M’ or ‘E’.
Code
DFS
class Solution {
int[][] dirs = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
public char[][] updateBoard(char[][] board, int[] click) {
int x = click[0];
int y = click[1];
char curr = board[x][y];
if (curr == 'M') {
board[x][y] = 'X';
return board;
}
int count = findMines(board, x, y);
if (count == 0) {
reveal(board, x, y);
} else {
// 如果周围有雷 就把当前的位置变成雷的数量 然后返回
board[x][y] = (char) (count + '0');
}
return board;
}
private int findMines(char[][] board, int i, int j) {
int count = 0;
for (int[] dir : dirs) {
int x = i + dir[0];
int y = j + dir[1];
if (x >= 0 && x < board.length && y >= 0 && y < board[0].length) {
if (board[x][y] == 'M' || board[x][y] == 'X') {
count++;
}
}
}
return count;
}
private void reveal(char[][] board, int i, int j) {
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return;
if (board[i][j] == 'M' || board[i][j] == 'B') return;
int count = findMines(board, i, j);
if (count != 0) {
board[i][j] = (char) (count + '0');
return;
}
board[i][j] = 'B';
for (int[] dir : dirs) {
reveal(board, i + dir[0], j + dir[1]);
}
}
}
BFS
class Solution {
int[][] dirs = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
public char[][] updateBoard(char[][] board, int[] click) {
int x = click[0];
int y = click[1];
if (board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}
int count = findMines(board, x, y);
if (count != 0) {
board[x][y] = (char) (count + '0');
return board;
}
Queue<int[]> queue = new LinkedList<>();
queue.offer(click);
while (!queue.isEmpty()) {
int[] curr = queue.poll();
board[curr[0]][curr[1]] = 'B';
for (int[] dir : dirs) {
int i = curr[0] + dir[0];
int j = curr[1] + dir[1];
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) continue;
if (board[i][j] != 'E') continue;
int currCount = findMines(board, i, j);
if (currCount != 0) {
board[i][j] = (char) (currCount + '0');
continue;
}
board[i][j] = 'B';
queue.offer(new int[]{i, j});
}
}
return board;
}
private int findMines(char[][] board, int i, int j) {
int count = 0;
for (int[] dir : dirs) {
int x = i + dir[0];
int y = j + dir[1];
if (x >= 0 && x < board.length && y >= 0 && y < board[0].length) {
if (board[x][y] == 'M' || board[x][y] == 'X') {
count++;
}
}
}
return count;
}
}
按 <- 键看上一题!
528. Random Pick with Weight
按 -> 键看下一题!
530. Minimum Absolute Difference in BST