题解 | #迷宫问题#

迷宫问题

http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

DFS 深度优先搜索


def dfs(matrix, x, y, path, dst):
    if (x,y) == dst:
        path.append((x,y))
        for i in path:
            print("({},{})".format(i[0],i[1]))
        return 
    
    
    if not 0<=x<len(matrix) or not 0<=y<len(matrix[0])\
    or matrix[x][y] == 1 or (x,y) in path:
        return
    
    path.append((x,y))
    dfs(matrix, x+1, y, path, dst)
    dfs(matrix, x, y+1, path, dst)
    dfs(matrix, x-1, y, path, dst)
    dfs(matrix, x, y-1, path, dst)
    path.pop()
while True:
    try:
        x, y = list(map(int, input().split()))
        dst = (x-1, y-1)
        matrix = []
        for i in range(x):
            matrix.append(list(map(int, input().split())))
        dfs(matrix, 0, 0, [], dst)
        
    except:
        break
        
        
全部评论
请问 第18行 path.pop() 是什么意思啊?
点赞 回复 分享
发布于 2022-11-10 22:40 广东

相关推荐

付费才包邮:本科有这种简历很强了
点赞 评论 收藏
分享
评论
13
1
分享

创作者周榜

更多
牛客网
牛客企业服务