529. Minesweeper(Leetcode每日一题-2020.08.20)

Problem

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.

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.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minesweeper
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Example1

529. Minesweeper(Leetcode每日一题-2020.08.20)_第1张图片

Example2

529. Minesweeper(Leetcode每日一题-2020.08.20)_第2张图片

Solution

class Solution {
public:

    vector<pair<int,int>> dirs = {
        {-1,0},{1,0},{0,-1},{0,1},
        {-1,-1},{-1,1},{1,-1},{1,1}
    };
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        int r = click[0];
        int c = click[1];
        if(board[r][c] == 'M')
        {
            board[r][c] = 'X';
            return board;
        }
        else if(board[r][c] == 'B')
        {
            return board;
        }

        int m = board.size();
        int n = board[0].size();
        int nbr_mine_cnt = 0;
        for(auto dir:dirs)
        {
            int x = r + dir.first;
            int y = c + dir.second;
            if(x >= 0 && x <m && y>=0 && y<n && board[x][y] == 'M')
                ++nbr_mine_cnt;
        }

        if(nbr_mine_cnt)
        {
            board[r][c] = '0' + nbr_mine_cnt;
            return board;
        }
        else
        {
            board[r][c] = 'B';
            for(auto dir:dirs)
            {
                int x = r + dir.first;
                int y = c + dir.second;
                if(x >= 0 && x <m && y>=0 && y<n)
                {
                    vector<int> nbr_click = {x,y};
                    updateBoard(board,nbr_click);
                }       
            }

            return board;
        }


    }
};

你可能感兴趣的:(letcode深度优先搜索)