题解 | #迷宫问题#

迷宫问题

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

递归

def func(map_, n, m, x, y, x1, y1):
    """
    n, x, x1: 行
    m, y, y1: 列
    """
    if x > n-1 or y > m-1 or x < 0 or y < 0 or map_[x][y] == 1:
        return False
    if x == n-1 and y == m-1:
        return [(x, y)]

    new_steps = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
    if (x1, y1) in new_steps:
        new_steps.remove((x1, y1))

    steps = [(x, y)]
    for new_step in new_steps:
        next_steps = func(map_, n, m, new_step[0], new_step[1], x, y)
        if next_steps != False:
            steps.extend(next_steps)
            return steps
    return False


a = input().split()
n1 = int(a[0])
m1 = int(a[1])

map_ = []
for i in range(n1):
    map_.append(list(map(int, input().split())))

steps = func(map_, n1, m1, 0, 0, -1, 0)
for step in steps:
    print("({},{})".format(step[0], step[1]))

全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务