题解 | 迷宫问题(回溯,抄的)
dx = [0, 0, -1, 1] dy = [-1, 1, 0, 0] def dfs(i, j): if i == m - 1 and j == n - 1: for (pi, pj) in pos: print('({0},{1})'.format(pi, pj)) return for k in range(4): x = i + dx[k] y = j + dy[k] if 0 <= x < m and 0 <= y < n and mat[x][y] == 0: pos.append((x, y)) mat[x][y] = 1 dfs(x, y) mat[x][y] = 0 pos.pop() m, n = map(int, input().strip().split()) mat = [] for i in range(m): mat.append(list(map(int, input().split()))) pos = [(0, 0)] # print(mat) dfs(0, 0)