广度优先搜索|130, 529

广度优先搜索|130. 被围绕的区域,529. 扫雷游戏

被围绕的区域

也是得倒流,做法和深度优先其实差不多。

python 复制代码
class Solution:
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        def bfs(queue,board):
            while queue:
                x, y = queue.popleft()
                for k1,k2 in [[x+1,y],[x-1,y],[x,y+1],[x,y-1]]:
                    if 0<=k1<m and 0<=k2<n and board[k1][k2] == 'O':
                        queue.append([k1,k2])
                        board[k1][k2] = 'A'
            return board 
        
        m = len(board)
        n = len(board[0])
        queue = deque()
        for i in range(m):
            if board[i][0] == 'O':
                queue.append([i,0])
                board[i][0] = 'A'
            if board[i][n-1] == 'O':
                queue.append([i,n-1])
                board[i][n-1] = 'A'
        for j in range(n):
            if board[0][j] == 'O':
                queue.append([0,j])
                board[0][j] = 'A'
            if board[m-1][j] == 'O':
                queue.append([m-1,j])
                board[m-1][j] = 'A'

        
        board = bfs(queue,board)

        for i in range(m):
            for j in range(n):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == 'A':
                    board[i][j] = 'O'

扫雷游戏

这里没写visit数列,所以在列队走到那里之后还是要改成B防止他反复进列队。

python 复制代码
class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        #direc = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
        m = len(board)
        n = len(board[0])

        def empty(i,j):
            boom = 0
            for k1,k2 in [[i+1,j],[i-1,j],[i,j+1],[i,j-1],[i+1,j+1],[i-1,j+1],[i-1,j-1],[i+1,j-1]]:
                #x = i+k1
                #y = j+k2
                if (0<=k1<m and 0<=k2<n) and board[k1][k2] == 'M':
                    boom += 1
            return boom
        
        queue = deque() 
        queue.append([click[0],click[1]])

        while queue:
            x,y = queue.popleft()
            if board[click[0]][click[1]] == 'M':
                board[click[0]][click[1]] = 'X'
                return board  

            num_b = empty(x,y)
            if not num_b:
                board[x][y] = 'B'
            else:
                board[x][y] = str(num_b)
                continue

            for k1,k2 in [[x+1,y],[x-1,y],[x,y+1],[x,y-1],[x+1,y+1],[x-1,y+1],[x-1,y-1],[x+1,y-1]]:
                if 0<=k1<m and 0<=k2<n and board[k1][k2]=='E':
                    queue.append([k1,k2])
                    board[k1][k2] = 'B' #这里改B是因为改了之后这个点就不会重复进队了!
               
        return board
相关推荐
段一凡-华北理工大学4 小时前
工业智能体:从Codex/Claude到项目实战~系列文章05:从“AI助手“到“工业智能体“:能力跃迁与落地范式
人工智能·python·claude·codex·工业智能体·高炉炼铁智能化
深蓝AI4 小时前
ContextVar 到线程池就丢了?asyncio.to_thread 和 run_in_executor 实测
python
岁月宁静5 小时前
什么是 Agent:从"会聊天"到"会做事"
人工智能·python
岁月宁静6 小时前
AI 应用开发工程师简历怎么写:HR 初筛、技术复筛、面试追问,三道关卡各看什么?
人工智能·python·面试
来让爷抱一个6 小时前
Python 异步里混用同步库,事件循环被卡住的全过程
开发语言·python
Linux-lucky6 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
禁默6 小时前
启元 Q1 二次开发实践:Ubuntu 部署与仿真验证,从 PrimeConsole 到 ROS 2 Humble + RViz
linux·运维·ubuntu
skywalk81637 小时前
把 去也网站 从 Ubuntu 搬到 FreeBSD:一次 Service Jail + nginx 的部署实录
linux·运维·nginx·ubuntu·freebsd
小肝一下8 小时前
3.基础语法(3)
python
Dream it possible!8 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划