题解 | #迷宫问题#

迷宫问题

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

def dfs(x,y,path):
    if x==m-1 and y==n-1:
        return path
    paths=[]
    if x-1>=0 and graph[x-1][y]==0 and (x-1,y) not in path:
        paths.extend(dfs(x-1,y,path+[(x-1,y)]))
    if x+1<=m-1 and graph[x+1][y]==0 and (x+1,y) not in path:
        paths.extend(dfs(x+1,y,path+[(x+1,y)]))
    if y-1>=0 and graph[x][y-1]==0 and (x,y-1) not in path:
        paths.extend(dfs(x,y-1,path+[(x,y-1)]))
    if y+1<=n-1 and graph[x][y+1]==0 and (x,y+1) not in path:
        paths.extend(dfs(x,y+1,path+[(x,y+1)]))
    return paths    
while True:
    try:
        number=list(map(int,input().split()))
        m=number[0]
        n=number[1]
        graph=[]
        for i in range(m):
            graph.append(list(map(int,input().split())))
        path=[(0,0)]
        res=dfs(0,0,path)
        print("\n".join([f"({x},{y})" for x, y in res])) 
    except:
        break

全部评论

相关推荐

ArisRobert:统一解释一下,第4点的意思是,公司按需通知员工,没被通知到的员工是没法去上班的,所以只要没被通知到,就自动离职。就是一种比较抽象的裁员。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务