题解 | #迷宫问题#
迷宫问题
http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
python 深度优先,注意最后输出要是字符串,直接输出tuple会报错
while True:
try:
maze = []
x = input().split()
m,n = int(x[0]),int(x[1])
for i in range(m):
x = input().split()
x = list(map(int,x))
maze.append(x)
def dfs(x,y,path):
if not (0<=x<m and 0<=y<n) or maze[x][y]:
return
maze[x][y] = 1
path.append([x,y])
if x == m-1 and y == n-1:
global res
res = path[:]
for nx,ny in[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]:
dfs(nx,ny,path)
path.remove([x,y])
dfs(0,0,[])
#print(res)
for i in res:
print('(%d,%d)'%(i[0],i[1]))
except:
break

查看14道真题和解析