迷宫问题 暴力dfs
矩阵中的路径
http://www.nowcoder.com/questionTerminal/c61c6999eecb4b8f88a98f66b273a3cc
来个暴力dfs
def dfs(i, j, MG, I, J, lujin, temp): if i== I-1 and j ==J-1:#找到出口 递归结束 打印即可 for i in range(len(temp)): print("(%d,%d)"%(temp[i][0],temp[i][1])) return if i + 1 <I and MG[i+1][j]==0:#只往i+1 和j+1两个方向搜索 没有重复路径 dfs(i + 1, j, MG, I, J, lujin, temp + [(i + 1, j)]) if j + 1 <J and MG[i][j+1]==0: dfs(i, j + 1, MG, I, J, lujin, temp + [(i, j + 1)]) else:#剪枝 走进死胡同 return return while True: try: [a, b] = [int(i) for i in input().split()] m = [] for i in range(a): m.append([int(i) for i in input().split()]) lujin = [] temp = [] dfs(0, 0, m, a, b, lujin, temp + [(0, 0)]) except: break