529-Minesweeper

本文介绍了一种基于深度优先搜索的扫雷游戏算法实现。通过递归的方式,该算法能够根据玩家点击的位置揭示游戏面板上的方块,并正确显示相邻雷的数量。文章详细解释了算法流程及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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:

这里写图片描述

Example 2:
这里写图片描述


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’代表未曝光的雷, ‘E’代表未曝光的空块, ‘B’代表曝光的空块, 其在相邻处(上, 下, 左, 右, 以及4个斜线)没有雷, 数字(‘1’到’8’)代表一个曝光的方块相邻处有几个雷, ‘X’代表曝光的雷

现在给出下一步要点击的坐标(点击范围为未曝光的方块, ‘M’或者’E’), 根据如下规则曝光并返回面板:

  1. 如果’M’被曝光, 游戏结束-将其转换为’X’
  2. 如果’E’被曝光, 且其相邻处没有雷, 那么将其转换为’B’, 并且对其相邻的未曝光的方块递归曝光
  3. 如果’E’被曝光, 且其相邻处有至少一个雷, 那么将其转换为数字(‘1’到’8’), 代表相邻的地雷数
  4. 返回面板

问题分析

DFS


解法

class Solution {
    public char[][] updateBoard(char[][] board, int[] click) {
        //若点击处为'M', 游戏结束
        if(board[click[0]][click[1]] == 'M'){
            board[click[0]][click[1]] = 'X';
            return board;
        }

        int m = board.length, n = board[0].length;
        //相邻方向
        int[][] dirs = {{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}};
        updateFunc(board, m, n, click[0], click[1], dirs);

        return board;
    }

    public void updateFunc(char[][] board, int m, int n, int i, int j, int[][] dirs){
        //对相邻地雷进行计数
        int count = getAdjMine(board, m, n, i, j, dirs);
        int newi = 0, newj = 0;
        //若相邻处无地雷, 那么对相邻方向的空块进行递归, 否则, 转换当前方块为数字
        if(count == 0){
               board[i][j] = 'B';
               for(int[] dir : dirs){
                   newi = i + dir[0];
                   newj = j + dir[1];
                   if(newi >= 0 && newi < m && newj >= 0 && newj < n && board[newi][newj] == 'E'){
                       updateFunc(board, m, n, newi, newj, dirs);
                   }
               }
        }else if(count != 0){
            board[i][j] = (char)(48 + count);
        }
    }
    //相邻处地雷计数方法
    public int getAdjMine(char[][] board, int m, int n, int i, int j, int[][] dirs){
        int newi = 0, newj = 0;

        int count = 0;
        for(int[] dir : dirs){
            newi = i + dir[0];
            newj = j + dir[1];
            if(newi >= 0 && newi < m && newj >= 0 && newj < n && board[newi][newj] == 'M') count++;
        }

        return count;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值