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

解題方法:

一道挺無聊的題,具體步驟題目說明已經(jīng)給出,使用尾遞歸解決,并且LeetCode的程序也有點問題(比如在掃雷中數(shù)字是不能走的)。
能點的方塊有:'M', 'E'
不能點的方塊有:'B', '1~8', 'X'
點到M游戲結(jié)束,點到E如果周邊沒有地雷則繼續(xù)遞歸,如果有雷則顯示雷的數(shù)量。

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; //控制游戲能否繼續(xù)
        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;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 今天真是折磨的一天,很久都沒有因為喝多酒而這么難受了。昨晚一時的痛快換來的是今天一整天的痛苦??梢哉f已經(jīng)數(shù)不清是第...
    劍伴誰在閱讀 322評論 0 1
  • 1. 11月份家里有事,12月份單位要求加班,瑜伽練習一直斷斷續(xù)續(xù)。1月份以后,生活、工作慢慢恢復正常了,瑜伽練習...
    文曉玲閱讀 507評論 4 8
  • 毛血旺,起源于重慶,流行于重慶和四川地區(qū),是一道著名的傳統(tǒng)菜式,列入川菜菜譜之一,以鴨血為制作主料,毛血旺的烹飪技...
    博琳達閱讀 1,326評論 5 19
  • 好久沒用過單獨創(chuàng)建xib文件,突然創(chuàng)建竟然發(fā)現(xiàn)程序崩潰的不行不行的,查找后知道怎么回事了,現(xiàn)在記錄下來,以備不時之...
    周末小螞蟻閱讀 2,935評論 4 3

友情鏈接更多精彩內(nèi)容