干迷宫

#创建迷宫
maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
# lst[(1, 1), (2, 1), (3, 1)]
#设置起点
#设置终点

start = (1, 1)
end = (8, 8)

#判断当前这个点,上下左右是0 还是1 如果是0 可以走
#下:r + 1 ,c
#右:r, c+1
#左:r, c-1
#上:r-1,c


#栈:先进后出:
#list就是一个栈
# lst = []
# lst.append('胡辣汤')
# lst.append('张无忌')
# lst.append('灭绝')
# lst.append('张三')
lst = [start]

while lst:
#列表有东西才能继续向前走
now = lst[-1]
if now == end:
print('出来了')
print(lst)
break
row, col = now #解构
maze[row][col] = 2
if maze[row - 1][col] == 0:
lst.append((row - 1, col))
elif maze[row][col + 1] == 0:
lst.append((row, col + 1))
elif maze[row][col - 1] == 0:
lst.append((row, col - 1))
elif maze[row + 1][col] == 0:
lst.append((row + 1, col))
else:
lst.pop()


else:
print("迷宫是走不通的")

posted on 2019-09-11 21:04  yeers  阅读(365)  评论(0编辑  收藏  举报

导航