# 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:
Input:
[['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']]
Explanation: Example 2:
Input:
[['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']]
Explanation:

## 2. Implementation

(1) BFS
class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
if (board == null || board.length == 0 || board.length == 0) {
return board;
}
int[][] directions = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
int row = click, col = click;
while (!queue.isEmpty()) {
int[] curCell = queue.remove();
int curRow = curCell;
int curCol = curCell;
if (board[curRow][curCol] == 'M') {
board[curRow][curCol] = 'X';
}
else {
int mines = 0;
for (int[] direction : directions) {
int nextRow = curRow + direction;
int nextCol = curCol + direction;
if (isValid(nextRow, nextCol, board)) {
if (board[nextRow][nextCol] == 'M') {
++mines;
}
}
}
if (mines > 0) {
board[curRow][curCol] = (char)(mines + '0');
}
else {
board[curRow][curCol] = 'B';
for (int[] direction : directions) {
int nextRow = curRow + direction;
int nextCol = curCol + direction;
if (isValid(nextRow, nextCol, board)) {
if (board[nextRow][nextCol] == 'E') {
board[nextRow][nextCol] = 'B';
}
}
}
}
}
}
return board;
}
public boolean isValid(int row, int col, char[][] board) {
return row >= 0 && row < board.length && col >= 0 && col < board.length;
}
}
(2) DFS
public class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
if (board == null || board.length == 0 || board.length == 0) {
return board;
}
int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
int row = click, col = click;
if (board[row][col] == 'M') {
board[row][col] = 'X';
}
else {
int mines = 0;
for (int[] direction : directions) {
int nextRow = row + direction;
int nextCol = col + direction;
if (isValid(board, nextRow, nextCol)) {
if (board[nextRow][nextCol] == 'M') {
++mines;
}
}
}
if (mines > 0) {
board[row][col] = (char)(mines + '0');
}
else {
board[row][col] = 'B';
for (int[] direction : directions) {
int nextRow = row + direction;
int nextCol = col + direction;
if (isValid(board, nextRow, nextCol)) {
if (board[nextRow][nextCol] == 'E') {
updateBoard(board, new int[] {nextRow, nextCol});
}
}
}
}
}
return board;
}
public boolean isValid(char[][] board, int row, int col) {
return row >= 0 && row < board.length && col >= 0 && col < board.length;
}
}

## 3. Time & Space Complexity

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