迷宫求解Python
时间: 2023-11-19 20:51:36 浏览: 30
好的,关于迷宫求解,你可以使用深度优先搜索(DFS)或广度优先搜索(BFS)算法来解决。下面是一个简单的DFS算法的Python实现:
```python
def dfs(maze, start, end):
stack = [start]
visited = set()
while stack:
x, y = stack.pop()
if (x, y) == end:
return True
if (x, y) in visited:
continue
visited.add((x, y))
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nx, ny = x + dx, y + dy
if 0 <= nx < len(maze) and 0 <= ny < len(maze[0]) and maze[nx][ny] != '#' and (nx, ny) not in visited:
stack.append((nx, ny))
return False
```
其中,maze是一个二维列表,表示迷宫;start和end分别是起点和终点的坐标。