1. 程式人生 > >leetcode 529. Minesweeper 掃雷遊戲 + 經典的DFS深度優先遍歷

leetcode 529. Minesweeper 掃雷遊戲 + 經典的DFS深度優先遍歷

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:

If a mine (‘M’) is revealed, then the game is over - change it to ‘X’.
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.
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.
Return the board when no more squares will be revealed.
Example 1:
Input:

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

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’]]

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:
The range of the input matrix’s height and width is [1,50].
The click position will only be an unrevealed square (‘M’ or ‘E’), which also means the input board contains at least one clickable square.
The input board won’t be a stage when game is over (some mines have been revealed).
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.

輸入的位置有兩種可能:
1. ‘M’,說明此處是地雷,該位置置為’X’,遊戲結束,返回board;
2. ‘E’,說明此處沒有地雷,這種又分為:
(1) 周圍8鄰接範圍有地雷,輸出數字
(2) 周圍8鄰接範圍沒有地雷,輸出’B’,翻開周圍8個方塊。

第一種情況可以直接輸出然後遊戲結束;而第二種情況需要用到DFS。
從這個方塊訪問周圍的8個方塊,如果有地雷,則count++,輸出數字;如果沒有地雷,輸出’B’,然後需要將周圍的8個方塊都翻開,此處用DFS遍歷這些結方塊,確定這些方塊的狀態。

直接DFS深度優先遍歷即可

程式碼如下:

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>

using namespace std;



class Solution
{
public:
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
    {
        int x = click[0], y = click[1];
        if (board[x][y] == 'M')
            board[x][y] = 'X';
        else
            dfs(board, x, y);
        return board;
    }

    void dfs(vector<vector<char>>& board, int x, int y)
    {
        if (isValid(x, y, board) == false)
            return;
        else if (board[x][y] == 'E')
        {
            vector<vector<int> > surr = { { -1,0 },{ -1,-1 },{ -1,1 },{ 1,0 },{ 1,-1 },{ 1,1 },{ 0,-1 },{ 0,1 } };
            int count = 0;
            for (vector<int> i : surr)
            {
                int x1 = x + i[0], y1 = y + i[1];
                if (isValid(x1, y1, board) == true && board[x1][y1] == 'M')
                    count++;
            }

            if (count >= 1)
            {
                board[x][y] = '0' + count;
                return;
            }
            else
            {
                board[x][y] = 'B';
                for (vector<int> i : surr)
                {
                    int x1 = x + i[0], y1 = y + i[1];
                    dfs(board, x1, y1);
                }
            }
        }
    }

    bool isValid(int x, int y, vector<vector<char>>& board)
    {
        int row = board.size(), col = board[0].size();
        if (x < 0 || x >= row || y < 0 || y >= col)
            return false;
        else
            return true;
    }
};