1

I have the same problem, like this question.

Sadly I need all possible paths between two fields in a hexagonal grid in exactly n steps, but without loops. Right now, if n_steps is much greater than the shortest path between those fields, I will have lots of loops.

I am struggling to find a fitting recursive function, which will help me write down the paths in a list of lists.

My function looks like this:

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

Anyone has a solution for this problem?

Community
  • 1
  • 1
Ali
  • 761
  • 1
  • 5
  • 24

0 Answers0