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. If a mine ('M') is revealed, then the game is over - change it to 'X'.

  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. 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. 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:
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[0].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[0], col = click[1];

        Queue<int[]> queue = new LinkedList<>();
        queue.add(click);

        while (!queue.isEmpty()) {
            int[] curCell = queue.remove();
            int curRow = curCell[0];
            int curCol = curCell[1];

            if (board[curRow][curCol] == 'M') {
                board[curRow][curCol] = 'X';
            }
            else {
                int mines = 0;
                for (int[] direction : directions) {
                    int nextRow = curRow + direction[0];
                    int nextCol = curCol + direction[1];

                    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[0];
                        int nextCol = curCol + direction[1];

                        if (isValid(nextRow, nextCol, board)) {
                            if (board[nextRow][nextCol] == 'E') {
                                board[nextRow][nextCol] = 'B';
                                queue.add(new int[] {nextRow, nextCol});
                            }
                        }
                    }
                }
            }
        }
        return board;
    }

    public boolean isValid(int row, int col, char[][] board) {
        return row >= 0 && row < board.length && col >= 0 && col < board[0].length;
    }
}

(2) DFS

public class Solution {
    public char[][] updateBoard(char[][] board, int[] click) {
        if (board == null || board.length == 0 || board[0].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[0], col = click[1];

        if (board[row][col] == 'M') {
            board[row][col] = 'X';
        }
        else {
            int mines = 0;

            for (int[] direction : directions) {
                int nextRow = row + direction[0];
                int nextCol = col + direction[1];

                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[0];
                    int nextCol = col + direction[1];

                    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[0].length;
    }
}

3. Time & Space Complexity

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

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

Last updated