题解 | 迷宫问题

迷宫问题

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



h, w = [int(i) for i in input().split()]
std = []
for i in range(h):
    row = [int(i) for i in input().split()]
    std.append(row)

def walk(x, y, pos=[(0, 0)]):
    if x + 1 < w and std[y][x+1] == 0:
        if (y, x+1) not in pos:
            walk(x+1, y, pos=pos+[(y, x+1)])
    if x - 1 >= 0 and std[y][x-1] == 0:
        if (y, x-1) not in pos:
            walk(x-1, y, pos=pos+[(y, x-1)])
    if y + 1 < h and std[y+1][x] == 0:
        if (y+1, x) not in pos:
            walk(x, y+1, pos=pos+[(y+1, x)])
    if y - 1 >= 0 and std[y-1][x] == 0:
        if (y-1, x) not in pos:
            walk(x, y-1, pos=pos+[(y-1, x)])
    if x == w - 1 and y == h - 1:
        for i in pos:
            print("".join(str(i).split()))

walk(0, 0)


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务