题解 | #迷宫问题# [男儿须读五车书]

迷宫问题

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

def dfs(i,j):
    dx=[0,0,-1,1]
    dy=[-1,1,0,0]
    if i==m-1 and j==n-1:
        for pos in route:
            print('('+str(pos[0])+','+str(pos[1])+')')

    for k in range(4):
        x=i+dx[k]
        y=j+dy[k]
        if x>=0 and x<m and y>=0 and y<n and map1[x][y]==0:
            map1[x][y]=1
            route.append((x,y))
            dfs(x,y)
            # 恢复现场
            map1[x][y]=0
            route.pop()

m,n=list(map(int,input().split()))
map1=[]
for i in range(m):
    s=list(map(int,input().split()))
    map1.append(s)
route=[(0,0)]
map1[0][0]=1
dfs(0,0)

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务