529 Minesweeper

529. Minesweeper

1. Question

You are given a 2D char matrix representing the game board.'M' represents an unrevealed mine,'E' represents an unrevealed empty square,'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines,digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.
Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:
  1. 1.
    If a mine ('M') is revealed, then the game is over - change it to 'X'.
  2. 2.
    If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent
    unrevealed squares should be revealed recursively.
  3. 3.
    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.
  4. 4.
    Return the board when no more squares will be revealed.
Example 1:
1
Input:
2
3
[['E', 'E', 'E', 'E', 'E'],
4
['E', 'E', 'M', 'E', 'E'],
5
['E', 'E', 'E', 'E', 'E'],
6
['E', 'E', 'E', 'E', 'E']]
7
8
Click : [3,0]
9
10
11
Output:
12
13
[['B', '1', 'E', '1', 'B'],
14
['B', '1', 'M', '1', 'B'],
15
['B', '1', '1', '1', 'B'],
16
['B', 'B', 'B', 'B', 'B']]
17
18
Explanation:
Copied!
Example 2:
1
Input:
2
3
[['B', '1', 'E', '1', 'B'],
4
['B', '1', 'M', '1', 'B'],
5
['B', '1', '1', '1', 'B'],
6
['B', 'B', 'B', 'B', 'B']]
7
8
Click : [1,2]
9
10
11
Output:
12
13
[['B', '1', 'E', '1', 'B'],
14
['B', '1', 'X', '1', 'B'],
15
['B', '1', '1', '1', 'B'],
16
['B', 'B', 'B', 'B', 'B']]
17
18
19
Explanation:
Copied!

2. Implementation

(1) BFS
1
class Solution {
2
public char[][] updateBoard(char[][] board, int[] click) {
3
if (board == null || board.length == 0 || board[0].length == 0) {
4
return board;
5
}
6
7
int[][] directions = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
8
9
int row = click[0], col = click[1];
10
11
Queue<int[]> queue = new LinkedList<>();
12
queue.add(click);
13
14
while (!queue.isEmpty()) {
15
int[] curCell = queue.remove();
16
int curRow = curCell[0];
17
int curCol = curCell[1];
18
19
if (board[curRow][curCol] == 'M') {
20
board[curRow][curCol] = 'X';
21
}
22
else {
23
int mines = 0;
24
for (int[] direction : directions) {
25
int nextRow = curRow + direction[0];
26
int nextCol = curCol + direction[1];
27
28
if (isValid(nextRow, nextCol, board)) {
29
if (board[nextRow][nextCol] == 'M') {
30
++mines;
31
}
32
}
33
}
34
35
if (mines > 0) {
36
board[curRow][curCol] = (char)(mines + '0');
37
}
38
else {
39
board[curRow][curCol] = 'B';
40
for (int[] direction : directions) {
41
int nextRow = curRow + direction[0];
42
int nextCol = curCol + direction[1];
43
44
if (isValid(nextRow, nextCol, board)) {
45
if (board[nextRow][nextCol] == 'E') {
46
board[nextRow][nextCol] = 'B';
47
queue.add(new int[] {nextRow, nextCol});
48
}
49
}
50
}
51
}
52
}
53
}
54
return board;
55
}
56
57
public boolean isValid(int row, int col, char[][] board) {
58
return row >= 0 && row < board.length && col >= 0 && col < board[0].length;
59
}
60
}
Copied!
(2) DFS
1
public class Solution {
2
public char[][] updateBoard(char[][] board, int[] click) {
3
if (board == null || board.length == 0 || board[0].length == 0) {
4
return board;
5
}
6
7
int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
8
9
int row = click[0], col = click[1];
10
11
if (board[row][col] == 'M') {
12
board[row][col] = 'X';
13
}
14
else {
15
int mines = 0;
16
17
for (int[] direction : directions) {
18
int nextRow = row + direction[0];
19
int nextCol = col + direction[1];
20
21
if (isValid(board, nextRow, nextCol)) {
22
if (board[nextRow][nextCol] == 'M') {
23
++mines;
24
}
25
}
26
}
27
28
if (mines > 0) {
29
board[row][col] = (char)(mines + '0');
30
}
31
else {
32
board[row][col] = 'B';
33
34
for (int[] direction : directions) {
35
int nextRow = row + direction[0];
36
int nextCol = col + direction[1];
37
38
if (isValid(board, nextRow, nextCol)) {
39
if (board[nextRow][nextCol] == 'E') {
40
updateBoard(board, new int[] {nextRow, nextCol});
41
}
42
}
43
}
44
}
45
}
46
return board;
47
}
48
49
public boolean isValid(char[][] board, int row, int col) {
50
return row >= 0 && row < board.length && col >= 0 && col < board[0].length;
51
}
52
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(mn), 空间复杂度O(mn)
DFS: 时间复杂度O(mn), 空间复杂度O(mn)