leetcode529. 扫雷游戏/dfs, bfs

题目:529. 扫雷游戏

让我们一起来玩扫雷游戏!

给定一个代表游戏板的二维字符矩阵。 ‘M’ 代表一个未挖出的地雷,‘E’ 代表一个未挖出的空方块,‘B’ 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖出的空白方块,数字(‘1’ 到 ‘8’)表示有多少地雷与这块已挖出的方块相邻,‘X’ 则表示一个已挖出的地雷。

现在给出在所有未挖出的方块中(‘M’或者’E’)的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板:

如果一个地雷(‘M’)被挖出,游戏就结束了- 把它改为 ‘X’。
如果一个没有相邻地雷的空方块(‘E’)被挖出,修改它为(‘B’),并且所有和其相邻的未挖出方块都应该被递归地揭露。
如果一个至少与一个地雷相邻的空方块(‘E’)被挖出,修改它为数字(‘1’到’8’),表示相邻地雷的数量。
如果在此次点击中,若无更多方块可被揭露,则返回面板。

示例 1:

输入:

[[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘M’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’]]

Click : [3,0]

输出:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

解释:Insert picture description here

示例 2:

输入:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Click : [1,2]

输出:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘X’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Explanation:

Insert picture description here

note:

  1. The range of the width and height of the input matrix is ​​[1,50].
  2. The clicked position can only be a block that has not been dug out ('M' or'E'), which also means that the panel contains at least one clickable block.
  3. The input panel will not be the state where the game is over (that is, mines have been dug out).
  4. For the sake of simplicity, rules not mentioned can be ignored in this question. For example, you don’t need to dig out all the mines when the game is over, consider all the situations where you might win the game or mark the squares.

Source: LeetCode
Link: https://leetcode-cn.com/problems/minesweeper The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea: dfs

class Solution {
public:
    int row, col;
    vector<vector<int>> dir;
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        dir = {
   
   {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
        row = board.size();
        if(row == 0)
            return board;
        col = board[0].size();
        dfs(board, click[0], click[1]);
        return board;
    }
    void dfs(vector<vector<char>> &board, int x, int y){
        if(board[x][y] == 'X' || (board[x][y] >= '1' && board[x][y] <= '8'))
            return;
        if(board[x][y] == 'M'){
            board[x][y] = 'X';
            return;
        }
        if(board[x][y] == 'E'){
            int cnt = 0;
            for(auto d : dir){
                int cx = x + d[0];
                int cy = y + d[1];
                
                if(judge_pos(cx, cy) && (board[cx][cy] == 'M' || board[cx][cy] == 'X') )
                    ++cnt;
                
            }
            if(cnt == 0){
                board[x][y] = 'B';
                for(int i = 0; i < 8; ++i){
                    int tx = x + dir[i][0];
                    int ty = y + dir[i][1];
                    if(judge_pos(tx, ty))
                        dfs(board, tx, ty);
                }                   
            }
            else{
                board[x][y] = ('0' + cnt);
            }
        }
        
    }
    bool judge_pos(int x, int y){
        return x >= 0 && x < row && y >= 0 && y < col;
    }
};

Of course, you can also use bfs for this question

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/108130298