#迷宫问题---栈和队列 #定义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)
栈和队列_迷宫问题_递归
最新推荐文章于 2020-10-17 17:15:06 发布