public IEnumerator <Caminho <Node> > GetEnumerator() { for (Caminho <Node> p = this; p != null; p = p.PreviousSteps) { yield return(p); } }
private Caminho(Node lastStep, Caminho <Node> previousSteps, double totalCost) { LastStep = lastStep; PreviousSteps = previousSteps; TotalCost = totalCost; }
static void Main(string[] args) { do { Grafo graph = new Grafo(); FillGraphWithGridMap(graph); DistanceType distanceType = DistanceType.km; // Prints on the screen the distance from a city to its neighbors. // Used mainly for debug information. DistanceBetweenNodes(graph, DistanceType.km); Console.WriteLine("Essas são as cidades que você pode escolher como Origem e Destino na Roménia: \n"); // Prints on screen the cities that you can choose as Start and Destination. foreach (Node n in graph.Nodes.Cast <Node>().OrderBy(n => n.Key)) { Console.WriteLine(n.Key); } string startCity = GetStartCity(graph); string destinationCity = GetDestinationCity(graph); Node start = graph.Nodes[startCity]; Node destination = graph.Nodes[destinationCity]; // Function which tells us the exact distance between two neighbours. Func <Node, Node, double> distance = (node1, node2) => node1.Neighbors.Cast <Vertices>().Single( etn => etn.Neighbor.Key == node2.Key).Cost; // Estimation/Heuristic function (Haversine distance) // It tells us the estimated distance between the last node on a proposed path and the destination node. Func <Node, double> haversineEstimation = n => Haversine.Distance(n, destination, DistanceType.km); Caminho <Node> shortestPath = FindPath(start, destination, distance, haversineEstimation); Console.WriteLine("\nEste é o menor caminho baseado no algoritmo de busca A*:\n"); // Prints the shortest path. foreach (Caminho <Node> path in shortestPath.Reverse()) { if (path.PreviousSteps != null) { Console.WriteLine(string.Format("De {0, -15} para {1, -15} -> Custo total = {2:#.###} {3}", path.PreviousSteps.LastStep.Key, path.LastStep.Key, path.TotalCost, distanceType)); } } Console.Write("\nDeseja buscar novamente? Sim/Não? "); }while (Console.ReadLine().ToLower() == "sim"); }