前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode529. Minesweeper

leetcode529. Minesweeper

作者头像
眯眯眼的猫头鹰
发布2020-05-12 10:57:22
4840
发布2020-05-12 10:57:22
举报

题目要求

Let's play the minesweeper game (Wikipedia),online game)!

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:

代码语言:javascript
复制
[['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'M', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E']]

Click : [3,0]

Output:

代码语言:javascript
复制
[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Example 2:

Input:

代码语言:javascript
复制
[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Click : [1,2]

Output:

代码语言:javascript
复制
[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'X', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Note:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  3. The input board won't be a stage when game is over (some mines have been revealed).
  4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

玩过扫雷游戏的同学应该熟悉扫雷的规则,这题的目标在于模拟扫雷游戏,它给了一块已经完成一部分操作的扫雷面板,并且输入了下一步点击的位置。要求输出执行这次点击后面板的最新状态。

其中M表示隐藏的雷,X表示被点开的雷。E表示未被点开的空格,B表示被点开的空格,数字1到8表示距离该格子一圈一共有几个雷。

当玩家点击了格子后,一共有三种情况

  1. 点中了M,则将该格子更新为X,游戏结束
  2. 点中了B,且周围有雷,则记录该格子周围一共几个雷,并将数量标记到这个格子上
  3. 点中了B,且周围无雷,则继续向周围一圈排雷。

思路和代码

这是一道典型的通过广搜或是深搜解决的题目。广搜即以雷区为中心,每次向外搜索一圈,深搜则是搜索到全是雷区为止。广搜代码如下:

代码语言:javascript
复制
public char[][] updateBoard(char[][] board, int[] click) {  
    char clickValue = board[click[0]][click[1]];  
    if (clickValue == 'M') {  
        board[click[0]][click[1]] = 'X';  
    } else if (clickValue == 'E') {  
        //广度优先遍历  
  Queue<int[]> queue = new LinkedList<>();  
        queue.offer(click);  
        //8个方向  
  int[][] directions = new int[][]{  
            {0, 1},  
            {0, -1},  
            {-1, 0},  
            {1, 0},  
            {-1, -1},  
            {-1, 1},  
            {1, 1},  
            {1,-1}  
        };  
        while (!queue.isEmpty()) {  
            int[] tmpClick = queue.poll();  
            int countOfMine = 0;  
            List<int[]> emptyCells = new ArrayList<>();  
            for (int\[] direction : directions) {  
                int rowIndex = direction[0] + tmpClick[0];  
                int columnIndex = direction[1] + tmpClick[1];  
                if (rowIndex < 0 || rowIndex >= board.length || columnIndex < 0 || columnIndex >= board[0].length) {  
                    continue;  
                }  
                if (board[rowIndex][columnIndex] == 'M') {  
                    countOfMine++;  
                } else if (board[rowIndex][columnIndex] == 'E') {  
                   emptyCells.add(new int[]{rowIndex, columnIndex});  
                }  
            }  
            if (countOfMine == 0) {  
                board[tmpClick[0]][tmpClick[1]] = 'B';  
                //防止重复遍历,先将其置为B  
  for (int[] emptyCell : emptyCells) {  
                    board[emptyCell[0]][emptyCell[1]] = 'B';  
                }  
                queue.addAll(emptyCells);  
            } else {  
                board[tmpClick[0]][tmpClick[1]] = (char)('0' + countOfMine);  
            }  
        }  
    }  
    return board;  
}

深搜代码如下:

代码语言:javascript
复制
public char[][] updateBoard2(char[][] board, int[] click) { 
    int m = board.length, n = board[0].length;  
    int row = click[0], col = click[1];  
  
    if (board[row][col] == 'M') { // Mine  
  board[row][col] = 'X';  
    }  
    else { // Empty  
 // Get number of mines first.  int count = 0;  
        for (int i = -1; i < 2; i++) {  
            for (int j = -1; j < 2; j++) {  
                if (i == 0 && j == 0) continue;  
                int r = row + i, c = col + j;  
                if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;  
                if (board[r][c] == 'M' || board[r][c] == 'X') count++;  
            }  
        }  
  
        if (count > 0) { // If it is not a 'B', stop further DFS.  
  board[row][col] = (char)(count + '0');  
        }  
        else { // Continue DFS to adjacent cells.  
 //递归的进行深度优先遍历  board[row][col] = 'B';  
            for (int i = -1; i < 2; i++) {  
                for (int j = -1; j < 2; j++) {  
                    if (i == 0 && j == 0) continue;  
                    int r = row + i, c = col + j;  
                    if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;  
                    if (board[r][c] == 'E') updateBoard(board, new int[] {r, c});  
                }  
            }  
        }  
    }  
  
    return board;  
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路和代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档