def get_neighbors(cc, V, visited_nodes):
    connectivity = [(i, j) for i in [-1, 0, 1] for j in [-1, 0, 1] if not i ==
        j == 0]
neighbors = [tuple(e) for e in asarray(cc) - connectivity if e[0] > 0 and e
    [1] > 0 and e[0] < V.shape[0] and e[1] < V.shape[1]]
neighbors = [e for e in neighbors if e not in visited_nodes]
return neighbors

def extend_path(V, path, n):
    """
    Extend the given path with n steps
    """
path = [tuple(e) for e in path]
visited_nodes = set()
for _ in range(n):
    visited_nodes.update(path)
dist_min = numpy.inf
for i_cc, cc in enumerate(path[:-1]):
    neighbors = get_neighbors(cc, V, visited_nodes)
next_step = path[i_cc + 1]
next_neighbors = get_neighbors(next_step, V, visited_nodes)
join_neighbors = list(set(neighbors) & set(next_neighbors))
if len(join_neighbors) > 0:
    tentative_distance = [V[e] for e in join_neighbors]
argmin_dist = argmin(tentative_distance)
if tentative_distance[argmin_dist] < dist_min:
    dist_min, new_step, new_step_index = tentative_distance[argmin_dist
        ], join_neighbors[argmin_dist], i_cc + 1
path.insert(new_step_index, new_step)
return path
Вычислите кратчайший путь с ровно n узлами между двумя точками сетки.