Leetcode 529. Minesweeper (python)

题目

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:

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.

解法:

当碰到E的时候要特别注意:

  • 第一遍访问周围各个位置数M
  • 如果周围有M,那么不用将周围位置加入队列
  • 如果没有M,再访问一遍周围的位置,然后将没有visited过的位置加入队列
class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        m = len(board)
        n = len(board[0])
        
        visited = [[False]*n for _ in range(m)]
        
        q = collections.deque()
        q.append((click[0],click[1]))
        visited[click[0]][click[1]] = True
        dirs = [[0,1],[0,-1],[-1,0],[1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
        while q:
            i,j = q.popleft()
            if board[i][j] == 'M':
                board[i][j] = 'X'
                break
            if board[i][j] == 'E':
                count = 0
                for d in dirs:
                    x = i+d[0]
                    y = j+d[1]

                    if 0<=x<m and 0<=y<n and board[x][y]=='M':
                        count += 1
                if not count:
                    board[i][j] = 'B'
                    for d in dirs:
                        x = i+d[0]
                        y = j+d[1]

                        if 0<=x<m and 0<=y<n and not visited[x][y]:
                            q.append((x,y))
                            visited[x][y] = True
                else:
                    board[i][j] = str(count)
        return board

二刷C++版本

二刷的时候做了一些改进,首先不需要visited数组,当一个位置E被访问,一定会被修改为B或者是数字,所以只需要对所有E的位置进行BFS就可以。

class Solution {
public:
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        int m = board.size(), n = board[0].size();
        queue<pair<int,int>> q;
        q.push({click[0],click[1]});
        vector<pair<int,int>> dirs{{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
        int x, y, next_x, next_y,mines;
        vector<pair<int,int>> candidates;
        while(!q.empty()){
            x = q.front().first;
            y = q.front().second;
            q.pop();
            if(board[x][y] == 'M'){
                board[x][y] = 'X';
                return board;
            }
            if(board[x][y] == 'E'){
                mines = 0;
                candidates = {};
                for(auto d : dirs){
                    next_x = x + d.first;
                    next_y = y + d.second;
                    if(next_x >= 0 && next_x < m && next_y >= 0 && next_y < n){
                        if(board[next_x][next_y] == 'M'){
                            mines += 1;
                        }else if(board[next_x][next_y] == 'E'){
                            candidates.push_back({next_x,next_y});
                        }
                    }
                }
                if(mines > 0){
                    board[x][y] = mines + '0';
                }else{
                    board[x][y] = 'B';
                    for(auto it : candidates){
                        q.push(it);
                    }
                }
            }
        }
        return board;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值