在python中递归地查找所有可能的组合

2024-06-28 19:51:38 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一定数量的列表,每个列表代表从一个商店到所有其他商店所需的时间,以及一个包含时间间隔序列的列表,有没有一种方法可以使用递归性来查找商店之间所有可能的路线?在

例如

list_of_shops = [shop1, shop2, shop3, shop4] 
# Index for this list and the one below are the same

list_of_time_it_takes = [[0, 2, 1, 4], [2, 0, 1, 4], [2, 1, 0, 4], [1, 2, 3, 0]]
# the 0 indicates the index of the shop. It takes 0 minutes to reach the shop from itself.

list_of_time_intervals = [0, 2, 2]

商店只能参观一次。 我们可以看到,每隔2分钟就参观了3家商店,可能的路线是:

shop4 > shop2 > shop1

shop3 > shop1 > shop2

有没有一种方法可以使用与本页中介绍的递归方法类似的递归算法来获得上述输出?如果没有,获得上述输出的最佳方法是什么?在


Tags: ofthe方法列表time时间shop路线