aStarWithCost<T> function
AStarResult<T>
aStarWithCost<
T>( - Map<T, Map<T, num>> graph,
- T start,
- T goal,
- num heuristic(
- T node,
- T goal
), {
- 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);
}