Python 六边形网格,在n步中找到所有可能的路径
我也有同样的问题,比如问题 不幸的是,我需要一个六边形网格中两个字段之间的所有可能路径,精确地分为n步,但没有循环。现在,如果n_步数远大于这些字段之间的最短路径,我将有很多循环 我正在努力寻找一个合适的递归函数,它将帮助我在列表中写下路径 我的函数如下所示:Python 六边形网格,在n步中找到所有可能的路径,python,python-3.x,Python,Python 3.x,我也有同样的问题,比如问题 不幸的是,我需要一个六边形网格中两个字段之间的所有可能路径,精确地分为n步,但没有循环。现在,如果n_步数远大于这些字段之间的最短路径,我将有很多循环 我正在努力寻找一个合适的递归函数,它将帮助我在列表中写下路径 我的函数如下所示: def find_paths_from_to_with_length(self, pos_1,pos_2,n_steps): coordinate_1= self.find_coordinates(pos_1) neigh
def find_paths_from_to_with_length(self, pos_1,pos_2,n_steps):
coordinate_1= self.find_coordinates(pos_1)
neighbour_pos_1= self.coordinates[coordinate_1].neighbour_list
if n_steps == 1:
if pos_2 in neighbour_pos_1:
return [[(pos_1,pos_2)]]
return []
if pos_1 == pos_2:
return []
all_paths = []
for node in neighbour_pos_1:
neighbor_all_paths = self.find_paths_from_to_with_length(node,pos_2,n_steps-1)
for path in neighbor_all_paths:
print("PATH: ",path)
all_paths.append([(pos_1,node)] + path)
return all_paths
有人能解决这个问题吗