【发布时间】:2020-02-23 05:48:27
【问题描述】:
我试图解决一个问题,其中一部分是找到从 (0, 0) 到二维数组最右边的所有路径。这是我的代码:
def route_finder_helper(x, y, current_path, filler, list_of_lists):
current_path[filler] = (x, y)
if x == 0 and y == 0:
print(current_path)
list_of_lists.append(current_path)
return list_of_lists
if x == 0:
return route_finder_helper(x, y - 1, current_path, filler - 1, list_of_lists)
if y == 0:
return route_finder_helper(x - 1, y, current_path, filler - 1, list_of_lists)
return route_finder_helper(x-1, y, current_path, filler - 1, list_of_lists) + \
route_finder_helper(x, y-1, current_path, filler - 1, list_of_lists)
其中 x 和 y 是当前坐标,current_path 是当前路径的元组列表,filler 是要更改的列表位置的索引,list_of_lists 应该是所有路径。但是,当我运行这个程序并打印返回值时,我得到了这个输出:
[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)]
[(0, 0), (0, 1), (1, 1), (1, 2), (2, 2)]
[(0, 0), (1, 0), (1, 1), (1, 2), (2, 2)]
[(0, 0), (0, 1), (1, 1), (2, 1), (2, 2)]
[(0, 0), (1, 0), (1, 1), (2, 1), (2, 2)]
[(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)]
[[(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)]]
所以我得到了正确的路径,但我不知道如何将它们保存到列表列表中。有人可以帮我吗?
这就是我调用函数的方式:
x_coordinate = coordinates[0]
y_coordinate = coordinates[1]
path_length = (x_coordinate + 1) + (y_coordinate + 1) - 1
start_filling = path_length - 1
current_path = [0] * path_length
paths = route_finder_helper(x_coordinate, y_coordinate, current_path,
start_filling, [])
这是它应该返回的:
[[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)],[(0, 0), (0, 1), (1, 1), (1, 2), (2, 2)], [(0, 0), (1, 0), (1, 1), (1, 2), (2, 2)], [(0, 0), (0, 1), (1, 1), (2, 1), (2, 2)], [(0, 0), (1, 0), (1, 1), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)]]
【问题讨论】:
-
你能提供一个关于如何调用函数的例子吗?电流输出有什么问题?我猜最后一个(列表列表)不应该在那里?
-
x_coordinate = 坐标[0]; y_coordinate = 坐标[1]; path_length = (x_coordinate + 1) + (y_coordinate + 1) - 1; start_filling = path_length - 1;当前路径 = [0] * 路径长度;路径 = route_finder_helper(x_coordinate, y_coordinate, current_path, start_filling, [])
-
我不能把它换行,但我希望你能看到
-
是的,我明白,最好是编辑问题并将其添加到那里,但现在还可以。我很快就要睡觉了,我今晚可能没有时间“解决”这个问题。
-
感谢您编辑问题。我改变了答案,我想我解决了:)