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:


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

Link:

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

解题方法:

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

Tips:

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

完整代码:

vector<int> dirX = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dirY = {-1, 0, 1, -1, 1, -1, 0, 1};
class Solution 
{
public:
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) 
    {
        bool gameover = false; //控制游戏能否继续
        go(click[0], click[1], board, gameover);
        return board;
    }
    void go(int x, int y, vector<vector<char>>& 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<vector<char>>& board)
    {
        if(x < 0 || y < 0 || x >= board.size() || y >= board[0].size())
            return false;
        return true;
    }
    int countMine(int x, int y, vector<vector<char>>& 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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天真是折磨的一天,很久都没有因为喝多酒而这么难受了。昨晚一时的痛快换来的是今天一整天的痛苦。可以说已经数不清是第...
    剑伴谁在阅读 306评论 0 1
  • 1. 11月份家里有事,12月份单位要求加班,瑜伽练习一直断断续续。1月份以后,生活、工作慢慢恢复正常了,瑜伽练习...
    文晓玲阅读 482评论 4 8
  • 毛血旺,起源于重庆,流行于重庆和四川地区,是一道著名的传统菜式,列入川菜菜谱之一,以鸭血为制作主料,毛血旺的烹饪技...
    博琳达阅读 1,265评论 5 19
  • 好久没用过单独创建xib文件,突然创建竟然发现程序崩溃的不行不行的,查找后知道怎么回事了,现在记录下来,以备不时之...
    周末小蚂蚁阅读 2,896评论 4 3