Minesweeper解题报告

Description:

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.
**Example2: **
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:

Minesweeper解题报告_第1张图片

**Example1: **
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:
Minesweeper解题报告_第2张图片

Link:

https://leetcode.com/problems/minesweeper/#/description

解题方法:

一道挺无聊的题,具体步骤题目说明已经给出,使用尾递归解决,并且LeetCode的程序也有点问题(比如在扫雷中数字是不能走的)。
能点的方块有:'M', 'E'
不能点的方块有:'B', '1~8', 'X'
点到M游戏结束,点到E如果周边没有地雷则继续递归,如果有雷则显示雷的数量。

Tips:

整个递归过程中使用一个 bool变量gameover控制游戏能否进行。

完整代码:

vector dirX = {-1, -1, -1, 0, 0, 1, 1, 1};
vector dirY = {-1, 0, 1, -1, 1, -1, 0, 1};
class Solution 
{
public:
    vector> updateBoard(vector>& board, vector& click) 
    {
        bool gameover = false; //控制游戏能否继续
        go(click[0], click[1], board, gameover);
        return board;
    }
    void go(int x, int y, vector>& board, bool gameover)
    {
        if(!isValid(x, y, board) || gameover || (board[x][y] >= 48 && board[x][y] <= 56) || board[x][y] == 'X' || board[x][y] == 'B')
            return;
        if(board[x][y] == 'M')
        {
            gameover = true;
            board[x][y] = 'X';
            return;
        }      
        int cnt = countMine(x, y, board);
        if(cnt != 0)
        {
            board[x][y] = cnt + 48;
            return;
        }
        board[x][y] = 'B';
        for(int i = 0; i < 8; i++)
            go(x+dirX[i], y+dirY[i], board, gameover);
        
    }
    bool isValid(int x, int y, vector>& board)
    {
        if(x < 0 || y < 0 || x >= board.size() || y >= board[0].size())
            return false;
        return true;
    }
    int countMine(int x, int y, vector>& board)
    {
        int cnt = 0;
        for(int i = 0; i < 8; i++)
        {
            if(isValid(x + dirX[i], y + dirY[i], board) && board[x+dirX[i]][y + dirY[i]] == 'M')
                cnt++;
        }
        return cnt;
    }
};

你可能感兴趣的:(Minesweeper解题报告)