题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
# dfs,当且仅当路径可行添加途经点 import sys n,m=list(map(int,input().split())) matrix=[] for i in range(n): matrix.append(list(map(int,input().split()))) out=[] def dfs(i,j): if 0<=i<n and 0<=j<m and matrix[i][j]==0: matrix[i][j]=2 if dfs(i+1,j) or dfs(i,j+1) or dfs(i-1,j) or dfs(i,j-1) or (i==n-1 and j==m-1): out.append((i,j)) return True else: return False dfs(0,0) while out: tmp=out.pop() print('({},{})'.format(tmp[0],tmp[1]))#华为##dfs#