首页 > 其他 > 详细

美团-走迷宫

时间:2019-12-20 22:31:00      阅读:127      评论:0      收藏:0      [点我收藏+]
matric = [[1,0,0,1,0,1],
          [0,1,0,1,0,0],
          [0,0,0,0,0,1],
          [0,0,1,0,0,0],
          [0,0,0,0,1,0]]
b1,b2 = 1,0
e1,e2 = 4,5

rows,cols = len(matric),len(matric[0])
visited = [[0 for i in range(cols)] for j in range(rows)]

res = []
def find_path(matric,i,j,e1,e2,res):
    if i==e1 and j==e2:
        res.append((i,j))
        return True
    temp = False
    if i>=0 and i<rows and j>=0 and j<cols and visited[i][j]==0 and matric[i][j]==0:
        res.append((i,j))
        print(res)
        visited[i][j]=1
        temp = find_path(matric,i-1,j,e1,e2,res) or find_path(matric, i + 1, j, e1, e2,res) or find_path(matric, i, j-1, e1, e2,res) or find_path(matric, i, j+1, e1, e2,res)
        if not temp:
            res.pop()
    return temp

find_path(matric,b1,b2,e1,e2,res)
print(res)

  

美团-走迷宫

原文:https://www.cnblogs.com/kingshine007/p/12075138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!