【Leetcode】529. Minesweeper

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:

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:

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']]
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']]
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.


思路:

输入一个点击的位置,然后对点击的位置进行reveal操作,分两种情况:(1)如果点击的位置是‘M’,则将其变为‘X’;(2)如果点击的位置是‘E’,则再分为两种情况:(i)所reveal的位置周围都不存在‘M’,则变为‘B’,然后继续对其周围8个位置进行reveal的递归操作;(ii)所reveal的位置周围存在‘M’,则根据周围8个位置‘M’的数量,变为‘1’ ~ ‘8’ 的一个数字字符。由此可见,只有(2)(i)的操作需要进行递归操作,值得注意的是,递归操作每次对某个点周围所有的点进行递归,这是DFS算法的思想,在这里也可以用BFS算法的思想,不过不同的是采用队列结构来记录访问过的点。在递归时,为了更好地对所有点进行统一操作,可直接对点周围8个点进行判断,同时需要考虑某个点周围的点是否在棋盘格内,然后各自进行递归。另外,借助count变量来记录某个周围的‘M’的数量,然后该点的字符则为count+’0’。以下是C++用DFS算法的实现过程。

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

    bool inboard(const vector<vector<char>>& board, int x, int y){
        return ( x>=0 && x<board.size() && y>=0 && y<board[0].size() );
    }

    void reveal(vector<vector<char>>& board, int x, int y){
        if(!inboard(board,x,y)) return;//判断是否在棋盘格内
        if(board[x][y] == 'E'){
            int count = 0;
            for(int i=-1;i<2;i++){
                for(int j=-1;j<2;j++){
                    if(inboard(board,x+i,y+j) && board[x+i][y+j] == 'M') count++;
                }
            }

            if(count>0)
                board[x][y] = count+'0';
            else{
                board[x][y] = 'B';
                for(int i=-1;i<2;i++){
                    for(int j=-1;j<2;j++){
                        reveal(board,x+i,y+j);
                    }
                }
            }

        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值