Leetcode529. DFS之应用(三):解决扫雷游戏

Leetcode529. Minesweeper

题目

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:
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 adjacent unrevealed 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.

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

解题分析

看到这道题,千万不要被题目吓到了,其实这道扫雷游戏的题目并没有想象中的那么难,思路还算是比较直接清晰的。
首先根据题目的要求,我们知道初始情况下,地图只有两类可点击的位置,一类是地雷,一类是空位置。点击地雷的情况非常简单,直接将地雷对应的位置M改成X输出即可。点击空位置的情况就稍微复杂一些:一种情况是E变为B,一种情况是E变为周围地雷的个数,一种情况是保持不变。下面我们来分别分析这三种情况。
通过上面的例子我们不难分析出:首先对遍历的每个位置将其周边的位置进行遍历,如果周围的位置有地雷,就将该位置的E替换为周围地雷的个数。如果周围的位置没有地雷,就将E替换为B,并继续遍历其周围为E的位置,直到没有位置可以再暴露出来了,这样最后遍历完的结果就是最终输出的结果了。

源代码

class Solution {
public:
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        int height = board.size(), width = board[0].size(), row = click[0], col = click[1];
        if (board[row][col] == 'M') {
            board[row][col] = 'X';
        }
        else {
            int count = 0;
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    if (i == 0 && j == 0) {
                        continue;
                    }
                    int r = row + i, c = col + j;
                    if (r < 0 || r >= height || c < 0 || c >= width) {
                        continue;
                    }
                    if (board[r][c] == 'M') {
                        count++;
                    }
                }
            }
            if (count > 0) {
                board[row][col] = count + '0';
            }
            else {
                board[row][col] = 'B';
                for (int i = -1; i <= 1; i++) {
                    for (int j = -1; j <= 1; j++) {
                        if (i == 0 && j == 0) {
                            continue;
                        }
                        int r = row + i, c = col + j;
                        if (r < 0 || r >= height || c < 0 || c >= width) {
                            continue;
                        }
                        if (board[r][c] == 'E') {
                            vector<int> v;
                            v.push_back(r);
                            v.push_back(c);
                            updateBoard(board, v);
                        }
                    }
                }
            }
        }
        return board;
    }
};

以上是我对这道问题的一些想法,有问题还请在评论区讨论留言~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值