1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
the-maze.py 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
Sanghee Kim 提交于 6年前 . update the-maze.py
# Time: O(max(r, c) * w)
# Space: O(w)
import collections
class Solution(object):
def hasPath(self, maze, start, destination):
"""
:type maze: List[List[int]]
:type start: List[int]
:type destination: List[int]
:rtype: bool
"""
def neighbors(maze, node):
for i, j in [(-1, 0), (0, 1), (0, -1), (1, 0)]:
x, y = node
while 0 <= x + i < len(maze) and \
0 <= y + j < len(maze[0]) and \
not maze[x+i][y+j]:
x += i
y += j
yield x, y
start, destination = tuple(start), tuple(destination)
queue = collections.deque([start])
visited = set()
while queue:
node = queue.popleft()
if node in visited: continue
if node == destination:
return True
visited.add(node)
for neighbor in neighbors(maze, node):
queue.append(neighbor)
return False
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助