aStarWithCost<T> function

AStarResult<T> aStarWithCost<T>(
  1. Map<T, Map<T, num>> graph,
  2. T start,
  3. T goal,
  4. num heuristic(
    1. T node,
    2. T goal
    ), {
  5. int maxIterations = 10000,
})

Implementation

AStarResult<T> aStarWithCost<T>(
  Map<T, Map<T, num>> graph,
  T start,
  T goal,
  num Function(T node, T goal) heuristic, {
  int maxIterations = 10000,
}) {
  final path = aStar(
    graph,
    start,
    goal,
    heuristic,
    maxIterations: maxIterations,
  );

  if (path.isEmpty) return AStarResult<T>(path: [], cost: double.infinity);

  // Calculate total cost
  num totalCost = 0;
  for (int i = 0; i < path.length - 1; i++) {
    final current = path[i];
    final next = path[i + 1];
    totalCost += graph[current]![next]!;
  }

  return AStarResult<T>(path: path, cost: totalCost);
}