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



        
相关推荐
石山岭1 小时前
自己动手写了一个 Android 虚拟定位 App:GPSSimulate 技术实
android·前端
杉氧3 小时前
副作用 (Side Effects) 全攻略:如何像大师一样掌控 Composable 的生命周期?
android·架构·android jetpack
BothSavage5 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn5 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽6 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
Kapaseker7 小时前
Kotlin Toolchain 0.11 发布:主要是把 Amper 干没了
android·kotlin
三少爷的鞋9 小时前
Android 现代架构不需要事件总线进阶篇
android
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
杉氧1 天前
深入理解 Compose 重组机制:快照系统如何驱动 UI 精准刷新?
android·架构·android jetpack