题解——Leetcode 529. Minesweeper 难度:Medium

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 anunrevealed 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 thisrevealed square, and finally 'X' represents a revealed mine.

Now given the next click position (row and column indices) among all theunrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:

  1. If a mine ('M') is revealed, then the game is over - change it to'X'.
  2. If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacentunrevealed squares should be revealed recursively.
  3. 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.
  4. Return the board when no more squares will be revealed.

题目如上,简化后的题意如下:

扫雷游戏中'M'代表没有挖开的地雷,'X'代表挖开的地雷,'E'代表没有挖开的空白格,'B'代表挖开的空白格,数字代表当前挖开格的八邻域中地雷的数量。

输入为游戏板上某未挖开格('M'或'E')坐标,输出为挖开此格后游戏板各格的取值。

挖开格需要遵循以下规则:

1.如果挖到地雷,游戏结束,该格由'M'变为'X'。

2.如果挖到空白格且其八邻域中无地雷,该格由'E'变为'B',并且其八邻域中的每格递归地被挖开。

3.如果挖到空白格但其八邻域中有地雷,该格由'E'变为其八邻域中地雷的数量。

4.当没有格能被挖开时,游戏结束,返回游戏板。


C++程序如下:

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;
        }
        dfs(board, click[0], click[1]);
        return board;
    }
    
    bool inboard(vector<vector<char>> board, int x, int y){
        return x >= 0 && x < board.size() && y >= 0 && y < board[0].size();
    }
    
    void dfs(vector<vector<char>>& board, int x, int y){
        vector<vector<int>> a ={{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}};
        int count = 0;
        
        if(!inboard(board, x, y))
            return;
            
        if(board[x][y] == 'E'){
            for(int i = 0; i < 8; i++){
                if(inboard(board, x+a[i][0], y+a[i][1]) && board[x+a[i][0]][y+a[i][1]] == 'M')
                    count++;
            }
            if(count > 0)
                board[x][y] = '0' + count;
            else{
                board[x][y] = 'B';
                for(int i = 0; i < 8; i++)
                    dfs(board, x+a[i][0], y+a[i][1]);
            }
        }
    }
};
解题的关键在于运用递归的思想挖开未挖开格。主函数updateBoard()中第一个判断语句针对规则1,当挖开的不是地雷时递归地调用函数dfs()挖开更多空白格。当没有更多格被挖开时返回board。

函数dfs()是对规则2、3的处理,由于在统计八邻域中地雷个数时,位于边界的格的八邻域不都存在,所以加入inboard()函数判断格是否位于游戏板内。

if(board[x][y] == 'E')

当挖开的是空白格时,设置变量count统计其八邻域中地雷的数量,当count>0时,该格被赋予地雷的数量,不再挖开新的格;当count==0时,对其八邻域中的每格调用dfs()函数继续打开空白格,直到其八邻域中存在地雷为止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值