Leetcode 490 迷宫

python 复制代码
class Solution(object):
    def hasPath(self, maze, start, destination):
        """
        :type maze: List[List[int]]
        :type start: List[int]
        :type destination: List[int]
        :rtype: bool
        """
        #dfs
        m,n=len(maze),len(maze[0])
        visit=[[0 for _ in range(n)] for _ in range(m)]
        return self.dfs(maze,visit,start,destination)

    def dfs(self,maze,visit,start,destination):
        i,j=start[0],start[1]
        if visit[i][j]==1 or maze[i][j]==1:
            return False
        if destination==start:
            return True
        visit[i][j]=1

        pos=[(0,1),(0,-1),(-1,0),(1,0)]
        for item in pos:
            i,j=start[0],start[1]
            while 0<=i+item[0]<len(maze) and 0<=j+item[1]<len(maze[0]) and maze[i+item[0]][j+item[1]]==0:
                i,j=i+item[0],j+item[1]

            if self.dfs(maze,visit,[i,j],destination):
                return True
        return False



        

经过不用改visit,碰壁了改

python 复制代码
class Solution(object):
    def hasPath(self, maze, start, destination):
        """
        :type maze: List[List[int]]
        :type start: List[int]
        :type destination: List[int]
        :rtype: bool
        """
        #bfs
        m,n=len(maze),len(maze[0])

        visit=[[0 for _ in range(n)] for _ in range(m)]
        q=[(start[0],start[1])]
        visit[start[0]][start[1]]=1
        return self.bfs(maze,q,visit,destination)

    def bfs(self,maze,q,visit,destination):
        while q:
            cur=q.pop(0)
            # cur=[row,col]
            if destination[0]==cur[0] and destination[1]==cur[1]:
                return True
            # visit[cur[0]][cur[1]]=1
            pos=[(0,1),(0,-1),(1,0),(-1,0)]
            for item in pos:
                i,j=cur[0],cur[1]
                while i+item[0]>=0 and i+item[0]<len(maze) and j+item[1]>=0 and j+item[1]<len(maze[0]) and maze[i+item[0]][j+item[1]]==0:
                    i+=item[0]
                    j+=item[1]
                if visit[i][j]==0:
                    q.append([i,j])
                    visit[i][j]=1
        return False
            
        






    #     #dfs
    #     m,n=len(maze),len(maze[0])
    #     visit=[[0 for _ in range(n)] for _ in range(m)]
    #     return self.dfs(maze,visit,start,destination)

    # def dfs(self,maze,visit,start,destination):
    #     i,j=start[0],start[1]
    #     if visit[i][j]==1 or maze[i][j]==1:
    #         return False
    #     if destination==start:
    #         return True
    #     visit[i][j]=1

    #     pos=[(0,1),(0,-1),(-1,0),(1,0)]
    #     for item in pos:
    #         i,j=start[0],start[1]
    #         while 0<=i+item[0]<len(maze) and 0<=j+item[1]<len(maze[0]) and maze[i+item[0]][j+item[1]]==0:
    #             i,j=i+item[0],j+item[1]

    #         if self.dfs(maze,visit,[i,j],destination):
    #             return True
    #     return False



        
相关推荐
CV-Climber15 分钟前
检索技术的实际应用
人工智能·算法
hhzz1 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_1 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI2 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet2 小时前
关于图算法中的连通分量检测与最小割问题7
算法
爱笑鱼2 小时前
Binder(二):AIDL 生成的 Proxy、Stub 和 Parcel 到底在做什么?
android
心运软件3 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病3 小时前
牛客周赛 Round 153
python·算法
爱笑鱼3 小时前
Binder(一):一次方法调用,究竟怎样跨进程执行?
android
壮哥_icon4 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化