python + 递归
def find_path(maze,current,path,all_path): temp = path.copy() temp.append(current) if current[0] == len(maze)-1 and current[1] == len(maze[0])-1: if len(all_path) == 0: all_path.append(len(temp)) all_path.append(temp) else: if len(temp) < all_path[0]: all_path[0] = len(temp) all_path[1] = temp else: if current[0]-1 >= 0: if maze[current[0]-1][current[1]] == 0: next_ = (current[0]-1,current[1]) if next_ not in temp: find_path(maze,next_,temp,all_path) if current[0]+1 < len(maze): if maze[current[0]+1][current[1]] == 0: next_ = (current[0]+1,current[1]) if next_ not in temp: find_path(maze,next_,temp,all_path) if current[1]-1 >= 0: if maze[current[0]][current[1]-1] == 0: next_ = (current[0],current[1]-1) if next_ not in temp: find_path(maze,next_,temp,all_path) if current[1]+1 < len(maze[0]): if maze[current[0]][current[1]+1] == 0: next_ = (current[0],current[1]+1) if next_ not in temp: find_path(maze,next_,temp,all_path) while True: try: n,m = map(int, input().split()) maze = [] for i in range(n): maze.append(list(map(int, input().split()))) current = (0,0) path,all_path = [],[] find_path(maze,current,path,all_path) for j in all_path[1]: print ('(%d,%d)' % (j[0], j[1])) except: break