题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
def walk(i, j, pos=[(0, 0)]): if (j + 1 < m) and (maze[i][j + 1] == 0): if (i, j + 1) not in pos: walk(i, j + 1, pos + [(i, j + 1)]) if (j - 1 >= 0) and (maze[i][j - 1] == 0): if (i, j - 1) not in pos: walk(i, j - 1, pos + [(i, j - 1)]) if (i + 1 < n) and (maze[i + 1][j] == 0): if (i + 1, j) not in pos: walk(i + 1, j, pos + [(i + 1, j)]) if (i - 1 >= 0) and (maze[i - 1][j] == 0): if (i - 1, j) not in pos: walk(i - 1, j, pos + [(i - 1, j)]) if (i, j) == (n - 1, m - 1): for k, v in pos: print("(" + str(k) + "," + str(v) + ")") # n是行,m是列 n, m = list(map(int, input().split())) maze = [] for _ in range(n): maze.append(list(map(int, input().split()))) walk(0, 0)