您的位置:首页 > 其它

栈和队列_迷宫问题_递归

2017-01-02 00:56 232 查看
#迷宫问题---栈和队列

#定义maze
line_0 = [1,1,1,1,1,1,1]
line_1 = [1,1,0,1,1,1,1]
line_2 = [1,1,0,1,1,1,1]
line_3 = [1,1,0,0,0,1,1]
line_4 = [1,1,0,1,0,0,1]
line_5 = [1,1,0,1,1,1,1]
line_6 = [1,1,1,1,1,1,1]
maze=[line_1,line_1,line_2,line_3,line_4,line_5,line_6]
#方向
dirs = [(0,1),(1,0),(0,-1),(-1,0)]
def mark(maze,pos):
#定义到过了=2  maze是迷宫殿
maze[pos[0]][pos[1]]  = 2
def passable (maze,pos):
return maze[pos[0]][pos[1]] == 0

#递归
def find_path(maze,pos,end):
mark(maze,pos)
if pos == end:
return True
    else:
for i in range(4):
nextp = pos[0]+dirs[i][0],pos[1]+dirs[i][1]
#下一方向
if passable(maze,nextp):
find_path(maze,nextp,end)
print (nextp)
return True
        return False
find_path(maze,[5,2],[4,5])
print (maze)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: