美团-走迷宫

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)

  

posted @ 2019-12-20 21:28  尘世中一个迷途小书童  阅读(238)  评论(0编辑  收藏  举报