题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
def dfs(i,j):
'''
该函数作用为输出从坐标为i,j的点走到终点的路径
'''
#dx,dy用于控制探索方向,依次为下,上,左,右
dx = [0,0,-1,1]
dy = [-1,1,0,0]
if i == m-1 and j == n-1:
for pos in route:
print('('+str(pos[0])+','+str(pos[1])+')')
return
for k in range(4):#对上下左右四个方向便利
x = i+dx[k]
y = j+dy[k]
if x>=0 and x<m and y>=0 and y<n and map1[x][y]==0:#0代表该区域未探索
map1[x][y]=1
route.append((x,y))
dfs(x,y)
map1[x][y]=0
route.pop()
else:
return
while True:
try:
m,n = list(map(int,input().split()))
map1=[]
#导入map信息
for i in range(m):
s=list(map(int,input().split()))
map1.append(s)
# 初始值是(0,0)将其标记为已经访问
route = [(0,0)]
map1[0][0]=1
dfs(0, 0)
except:
break


