题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
# 2024年10月30日 周三 上午15:22 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 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 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 >= 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 = list(map(int, input().split())) maze = [] for i in range(n): maze.append(list(map(int, input().split()))) walk(0, 0)