You are given a 2D char matrix representing the game board.'M'represents anunrevealedmine,'E'represents anunrevealedempty square,'B'represents arevealedblank 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 thisrevealedsquare, and finally'X'represents arevealedmine.

Now given the next click position (row and column indices) among all theunrevealedsquares ('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']]

Solution:每poll出一个格,统计周围八个格地雷数,若空则置为B,同时把周围八个加入queue中,否则改为地雷数

    public char[][] updateBoard(char[][] board, int[] click) {
        if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || click == null || click.length < 2) {
            return board;
        }
        int[] dx = {1, 0, -1, 0, 1, 1, -1, -1};
        int[] dy = {0, 1, 0, -1, 1, -1, -1, 1};
        Queue<Node> queue = new LinkedList<>();
        if (board[click[0]][click[1]] == 'M') {
            board[click[0]][click[1]] = 'X';
            return board;
        }
        boolean[][] visited = new boolean[board.length][board[0].length];
        queue.offer(new Node(click[0], click[1]));
        visited[click[0]][click[1]] = true;
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            int count = 0;
            for (int i = 0; i < 8; i++) {
                int x = cur.x + dx[i], y = cur.y + dy[i];
                if (x >= 0 && x < board.length && y >= 0 && y < board[0].length) {
                    if (board[x][y] == 'M') {
                        count++;
                    }
                }
            }
            if (count == 0) {
                board[cur.x][cur.y] = 'B';
                for (int i = 0; i < 8; i++) {
                    int x = cur.x + dx[i], y = cur.y + dy[i];
                    if (x >= 0 && x < board.length && y >= 0 && y < board[0].length && !visited[x][y] && board[x][y] == 'E') {
                        queue.offer(new Node(x, y));
                        visited[x][y] = true;
                    }
                }

            } else {
                board[cur.x][cur.y] = (char)(count + '0');
            }
        }
        return board;
    }
    class Node {
        int x, y;
        public Node(int i, int j) {
            x = i;
            y = j;
        }
    }

或者可以先用count[][]把地雷附近的八个格先加上1

results matching ""

    No results matching ""