Leetcode 490 迷宫

请添加图片描述

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,碰壁了改

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



        
### LeetCode 490 题目解析 #### 题目描述 LeetCode490 题名为《迷宫》[^6]。题目给定一个由 `0` 和 `1` 组成的二维网格,其中 `0` 表示可以通过的空间,而 `1` 则表示障碍物。起点位于左上角 `(0, 0)`,终点位于右下角 `(m-1, n-1)`。需要判断是否存在一条路径可以从起点到达终点。 #### 解决方案概述 该问题可以使用广度优先搜索算法(BFS)来解决。通过 BFS 可以有效地遍历所有可能的路径并找到最短的一条通往目标位置的路。如果能够成功抵达,则返回 True;反之则返回 False。 #### 实现细节 为了实现上述思路,在 Python 中定义了一个辅助函数 `_bfs()` 来执行具体的 BFS 过程: ```python from collections import deque def hasPath(maze, start, destination): directions = [(0, 1), (0, -1), (-1, 0), (1, 0)] m, n = len(maze), len(maze[0]) queue = deque([start]) visited = set() visited.add((start[0], start[1])) while queue: curr_x, curr_y = queue.popleft() if [curr_x, curr_y] == destination: return True for dx, dy in directions: nx, ny = curr_x, curr_y # Roll until hitting wall or boundary while 0 <= nx + dx < m and 0 <= ny + dy < n and maze[nx + dx][ny + dy] == 0: nx += dx ny += dy if (nx, ny) not in visited: visited.add((nx, ny)) queue.append((nx, ny)) return False ``` 此代码片段展示了如何利用队列来进行层次化探索,并记录访问过的节点防止重复计算。每当遇到新的未被标记的位置时就将其加入到待处理列表中继续扩展直到发现目的地或者穷尽所有可能性为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值