leetcode 529. Minesweeper game (DFS)

leetcode 529. Minesweeper game (DFS)

Title description

Insert picture description here

Sample description

Insert picture description here

Analysis of Algorithms

There are many rules for this question, especially when I plan to do it in C at the beginning (I study cryptography and algorithm engineering is implemented in C, so I deliberately practice writing algorithms in C). As soon as I saw the template submitted by C language, I was silly, there were so many parameters... Write decisively in C++!

alright, do not piffle any more!

Refine the topic description

Give the location of the click and ask you what the game panel will look like after clicking~
If you accidentally click on a mine, an'X' will be displayed, and the game will end naturally (same as minesweeper~)
otherwise you need to deal with it There are a lot of things. Think about it. When you play minesweeper yourself, do you just vacate a large area at one point? This is processed according to this algorithm. What are the processing rules? ?

Rule 1:

If there are thunders around the point you clicked, then don't continue, just leave the number of thunders in eight directions and tell the player.

Rule 2:

If there is no thunder in the eight directions you have clicked, it will change from'E' to'B'. It is equivalent to telling the player that you have clicked, which is why your minesweeper game sometimes leaves a lot of space. Because there are no thunders around this location, it is directly displayed as'E'! !

Coding implementation

Okay, the rules are actually only two points, because I was too impetuous and didn't look into it at first~

Then how to code it? Give a click point, and then you can get the row r and column c of click. If this position is a thunder, it is directly expressed as'X', and this click is over! Otherwise, enter dfs()!

How to go to dfs specifically? First, the initialized panel only has'M' and'E', so being able to enter dfs() means that you are currently'E', so you need to judge whether there are thunders around it, and if so, find out how many, and then here Change the position of to a number! After changing to a number, this position does not need to continue anymore~ Otherwise, it means that there are no thunders around this position, so change it to'B', which means there is no thunder around and has been clicked! ! Then recurse in eight directions, and how to deal with the recursive exit? We can set up an array of status records, if the current point has been processed, then return, just fine!

Specific problem-solving code

class Solution {
    
    
public:
    int dir[8][2] = {
    
    {
    
    0, 1}, {
    
    0, -1}, {
    
    1, 0}, {
    
    -1, 0}, {
    
    1, 1}, {
    
    1, -1}, {
    
    -1, 1}, {
    
    -1, -1}};
    int vis[50][50] = {
    
    0};
    void dfs(int x, int y, vector<vector<char>>& board){
    
    
        if(vis[x][y])
            return ;
        vis[x][y] = 1;
        int cnt = 0;
        for(int i = 0;i < 8;++i){
    
    
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if(dx >= 0 && dx < (int)board.size() && dy >= 0 && dy < (int)board[0].size()){
    
    
                if('M' == board[dx][dy])
                    cnt++;
            }
        }
        if(cnt > 0){
    
    
            board[x][y] = cnt + '0';
        }
        else{
    
    
            board[x][y] = 'B';
            for(int i = 0;i < 8;++i){
    
    
                int dx = x + dir[i][0];
                int dy = y + dir[i][1];
                if(dx >= 0 && dx < (int)board.size() && dy >= 0 && dy < (int)board[0].size())
                    if(!vis[dx][dy])
                        dfs(dx, dy, board);
            }
        }
    }

    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
    
    
        int r = click[0], c = click[1];
        if('M' == board[r][c])
            board[r][c] = 'X';
        else
            dfs(r, c, board);
        return board;
    }

};

Submitted results

Insert picture description here
This performance is very unsatisfactory. I hope that there are senior colleagues who come up with better solutions.

Answer a question

Many people will ask, why is there an'E' right above the sample that has not been processed? ?
It's actually very simple, because starting from the position of the click, before encountering this'E', you will encounter locations with thunders around, and when you encounter these locations, the recursive processing will stop! So you can’t deal with this'E' ~
In fact, if you go to play the game of minesweeper, you will find that this is how people deal with it!

Write at the back

Due to my limited ability, if there are any mistakes or flaws, please feel free to enlighten me! Hope to get your advice by leaving a private message!

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/108134879