Exemple #1
0
        static void ProcessAnswers(TrainMap trainMap)
        {
            #region Processing
            // processango as respostas para o grafo
            var answer1  = trainMap.CalculateRouteDistance("ABC".ToCharArray());
            var answer2  = trainMap.CalculateRouteDistance("AD".ToCharArray());
            var answer3  = trainMap.CalculateRouteDistance("ADC".ToCharArray());
            var answer4  = trainMap.CalculateRouteDistance("AEBCD".ToCharArray());
            var result   = trainMap.CalculateRouteDistance("AED".ToCharArray());
            var answer5  = result == -1 ? "NO SUCH ROUTE" : result.ToString();
            var answer6  = trainMap.DiscoverRoutesByStops('C', 'C', 3, ComparisonOperations.ComparisonType.LessOrEqualThan);
            var answer7  = trainMap.DiscoverRoutesByStops('A', 'C', 4, ComparisonOperations.ComparisonType.EqualTo);
            var answer8  = trainMap.FindShortestPathDistance('A', 'C');
            var answer9  = trainMap.FindShortestPathDistance('B', 'B');
            var answer10 = trainMap.DiscoverRoutesByDistance('C', 'C', 30, ComparisonOperations.ComparisonType.LessThan);

            Console.WriteLine("Problem one - Trains");

            Console.WriteLine("Routes: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7");

            Console.WriteLine($"1. The distance of the route A-B-C = {answer1}");
            Console.WriteLine($"2. The distance of the route A-D = {answer2}");
            Console.WriteLine($"3. The distance of the route A-D-C {answer3}");
            Console.WriteLine($"4. The distance of the route A-E-B-C-D {answer4}");
            Console.WriteLine($"5. The distance of the route A-E-D {answer5}");
            Console.WriteLine($"6. The number of trips starting at C and ending at C with a maximum of 3 stops = {answer6}");
            Console.WriteLine($"7. The number of trips starting at A and ending at C with exactly 4 stops = {answer7}");
            Console.WriteLine($"8. The length of the shortest route (in terms of distance to travel) from A to C = {answer8}");
            Console.WriteLine($"9. The length of the shortest route (in terms of distance to travel) from B to B = {answer9}");
            Console.WriteLine($"10. The number of different routes from C to C with a distance of less than 30 = {answer10}");
            #endregion
        }
        public void ShouldFindShortestPath()
        {
            var stationA = new TrainStation('A');
            var stationB = new TrainStation('B');
            var stationC = new TrainStation('C');

            stationA.AddConnection(new TrainConnection(3, stationA, stationB));
            stationB.AddConnection(new TrainConnection(5, stationB, stationC));
            stationA.AddConnection(new TrainConnection(7, stationA, stationC));

            var stations = new List <TrainStation> {
                stationA, stationB, stationC
            };

            var trainMap = new TrainMap(stations);

            int shortestPath = trainMap.FindShortestPathDistance('A', 'C');

            Assert.Equal(shortestPath, 7);
        }