Leetcode of breadth-first search (BFS) Thematic -529. Minesweeper (Minesweeper)

Leetcode of breadth-first search (BFS) Thematic -529. Minesweeper (Minesweeper)

BFS Detailed entry: Leetcode of breadth-first search (BFS) Thematic -429 traversal sequence N-tree (N-ary Tree Level Order Traversal ).


Let's play Minesweeper!

Given a two-dimensional matrix represents the character of the game board. 'M' represents a non-excavated mine, 'E' represents a non-empty squares excavated, 'B' indicates no adjacent (upper, lower, left, right, and all four diagonal) has mines dug out of the box blank, digital ( '1' to '8') indicates how many mines have been dug out of the box with this neighbor, 'X' indicates that a mine has been dug up.

In all but now given excavated box ( 'M' or 'E') at a click position (row and column index), according to the following rules, a respective return position corresponding to the click panel:

  1. If a mine ( 'M') was dug up, the game is over - change it to 'X'.
  2. If a mine empty squares ( 'E') adjacent excavated, it is modified ( 'B'), and all of its neighboring block, and should be recursively disclosed.
  3. If a mine with at least one adjacent open squares ( 'E') is excavated, change it to a digital ( '1' to '8'), it represents the number of adjacent mines.
  4. If you click on this, the absence of more blocks may be revealed, the panel is returned.

 

Example 1:

Input: 

[[ 'E', 'E', 'E', 'E', 'E'], 
 [ 'E', 'E', 'M', 'E', 'E'], 
 [ 'E ',' E ',' E ',' E ',' E '], 
 [' E ',' E ',' E ',' E ',' E ']] 

the 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 ']] 

the 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:

 

note:

  1. Input matrix width and height in the range [1, 50].
  2. Click the location is not only dug out of the box ( 'M' or 'E'), this also means that the panel includes at least a clickable box.
  3. Input Panel will not be the end of the game state (ie mines have been dug up).
  4. 简单起见,未提及的规则在这个问题中可被忽略。例如,当游戏结束时你不需要挖出所有地雷,考虑所有你可能赢得游戏或标记方块的情况。

 

这题可以用DFS/BFS写,在BFS专题下,我们尝试用BFS求解这题:

思路如下:

1、从Click点开始

  • 如果click点是炸弹M,把这个点改成X,直接返回board
  • 如果不是M,把click点压入队列,并vis[click]==1,进入第2步

2、BFS

  • 计算这个点八个方向的地雷数量,如果数量>0,则把这个点修改为这个数量,返回board
  • 如果数量为0,则把八个方向上vis==0的点都压入队列,并把这些点的vis都置1
  • 再次进入2

 

AC代码:

class Solution {
    public static class POINT {
        int x, y;

        POINT(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    public int getM(char[][] board,int xx,int yy){
        int cnt = 0;
        for (int k = 0; k < 8; k++) {
            int newx = xx + dirx[k];
            int newy = yy + diry[k];
            if (newx >= 0 && newx < board.length && newy >= 0 && newy < board[0].length
                    && (board[newx][newy] == 'M' || board[newx][newy] == 'X')) {
                cnt++;
            }
        }
        return cnt;
    }
    int dirx[] = {0, 1, 1, 1, 0, -1, -1, -1};
    int diry[] = {1, 1, 0, -1, -1, -1, 0, 1};
    int[][] vis ;

    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;
        }
        vis = new int[board.length][board[0].length];
        Queue<POINT> queue = new LinkedList<>();
        vis[x][y] = 1;
        queue.offer(new POINT(x,y));
        while (!queue.isEmpty()) {
            POINT point = queue.poll();
            int xx = point.x;
            int yy = point.y;
            int cnt = getM(board,xx,yy); //附近炸弹数量
            if (cnt > 0) {
                board[xx][yy] = (char) (cnt + '0');
            } else {
                board[xx][yy] = 'B';
                for (int k = 0; k < 8; k++) {
                    int newx = xx + dirx[k];
                    int newy = yy + diry[k];
                    if (newx >= 0 && newx < board.length && newy >= 0 && newy < board[0].length && vis[newx][newy]==0) {
                        queue.offer(new POINT(newx,newy));
                        vis[newx][newy] = 1;
                    }
                }
            }

        }

        return board;
    }
}

 

Guess you like

Origin www.cnblogs.com/qinyuguan/p/11440019.html
Recommended