JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

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 = [clickr, clickc] 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:

  1. If a mine ‘M’ is revealed, then the game is over. You should change it to ‘X’.
  2. 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.
  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:

img

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:

img

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;
    }
}