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

给定一个二维字符矩阵表示扫雷游戏的棋盘。'M'表示未揭示的雷,'E'表示未揭示的空地,'B'表示已揭示的空格且周围没有雷,数字('1'到'8')表示已揭示的空格周围有多少颗雷,'X'表示已揭示的雷。当点击位置为未揭示的空地时,根据扫雷规则更新棋盘状态:如果点击的是雷,则游戏结束,该位置标记为'X';如果点击的是空地且周围没有雷,则变为揭示的空格'B'并递归揭示周围的空地;如果点击的是空地且周围有雷,则变为该雷的数量。返回最终的棋盘状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

在这里插入图片描述

Example2

在这里插入图片描述

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;
        }


    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值