题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
def dfs(i,j,path): if i+1 < m and maze[i+1][j] == 0 and ((i+1,j) not in path ): dfs(i+1,j,path+[(i+1,j)]) if i-1 >= 0 and maze[i-1][j] == 0 and ((i-1,j) not in path): dfs(i-1,j,path+ [(i-1,j)]) if j-1 >= 0 and maze[i][j-1] == 0 and ((i,j-1) not in path): dfs(i,j-1,path+ [(i,j-1)]) if j+1 < n and maze[i][j+1] == 0 and ((i,j+1) not in path): dfs(i,j+1,path + [(i,j+1)]) if i == m-1 and j == n-1: for i in range(len(path)): print(f'({path[i][0]},{path[i][1]})') m, n = list(map(int,input().strip().split())) maze = [] for i in range(m): maze.append(list(map(int,input().strip().split()))) dfs(0,0,[(0,0)])