题解 | #迷宫问题#

迷宫问题

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

row, column = map(int, input().split())
puzzle = []
for i in range(row):
    puzzle.append(list(map(int, input().split())))


def backtrack(r, c, steps):
    if r == row - 1 and c == column - 1:
        for line in steps + [(r, c)]:
            print(f'({line[0]},{line[1]})')
    else:
        direction = [(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)]
        for d in direction:
            if 0 <= d[0] < row and 0 <= d[1] < column:
                if puzzle[d[0]][d[1]] == 0 and d not in steps:
                    backtrack(d[0], d[1], steps + [(r, c)])


backtrack(0, 0, [])

简化了一下代码,各位大佬看看有没有什么问题

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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