stack - 深度优先搜索
1. 回溯法
2. 思路:从一个节点开始,任意找下一个能走的点,当找不到能走的点时,
退回上一个点寻找是否有其他方向的点
3. 使用 stack 存储当前路径
maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# (x, y) 四个方向 (URDL): (x, y+1), (x+1, y), (x, y-1), (x-1, y)
dirs = [
lambda x,y: (x, y+1),
lambda x,y: (x+1, y),
lambda x,y: (x, y-1),
lambda x,y: (x-1, y)
]
def maze_path_stack(x1, y1, x2, y2):
stack = []
stack.append((x1, y1))
while len(stack) > 0:
# 当前节点
curNode = stack[-1]
# 判断是否走到终点
if curNode[0] &