コード例 #1
0
        public virtual int GetDistanceOfShortestRoute(List<Track> railRoad, City origin, City destination)
        {
            var possibleRoutes = FindAllPossibleRoutes(railRoad, origin, destination);

            if (possibleRoutes.Count() == 0)
                throw new NoRouteException();

            return possibleRoutes.OrderBy(x => x.DistanceFromStart).First(x => x.DistanceFromStart != 0).DistanceFromStart;
        }
コード例 #2
0
 protected List<RouteWithDistanceFromStart> FindAllPossibleRoutes(List<Track> railRoad, City origin, City finalDestination)
 {
     if (RailRoadHasNoTracks(railRoad))
         throw new NoRouteException();
     FinalDestination = finalDestination;
     possibleRoutes = new List<RouteWithDistanceFromStart>();
     var initialRoute = StartRoute(origin);
     FindAllPossibleRoutes(railRoad, origin, initialRoute);
     return possibleRoutes;
 }
コード例 #3
0
        public virtual int GetNumberOfRoutesWithDistanceLimit(List<Track> railRoad, City origin, City destination, int distanceLimit)
        {
            this.distanceLimit = distanceLimit;
            var possibleRoutes = FindAllPossibleRoutes(railRoad, origin, destination);

            if (possibleRoutes.Count() == 0)
                throw new NoRouteException();

            return possibleRoutes.Where(x => x.DistanceFromStart < distanceLimit).Count();
        }
コード例 #4
0
ファイル: RouteFinder.cs プロジェクト: blackbnu/Trains
 protected virtual int FindNumberOfPossibleRoutes(List<Track> railRoad, City origin, City finalDestination)
 {
     if (RailRoadHasNoTracks(railRoad))
         throw new NoRouteException();
     possibleRoutesCounter = 0;
     FinalDestination = finalDestination;
     var initialRoute = StartRoute(origin);
     FindNumberOfPossibleRoutes(railRoad, origin, initialRoute);
     return possibleRoutesCounter;
 }
コード例 #5
0
ファイル: TrackTests.cs プロジェクト: blackbnu/Trains
        public void ShouldIdentifyIfTheTrackConnectTheGivenCities()
        {
            var origin = new City("A");
            var destination = new City("B");
            var otherCity = new City("C");

            var track = new Track(origin, destination, 4);

            track.IsAConnectionBetween(origin, destination).Should().BeTrue();
            track.IsAConnectionBetween(origin, otherCity).Should().BeFalse();
        }
コード例 #6
0
ファイル: Track.cs プロジェクト: blackbnu/Trains
 public static Track Parse(string track)
 {
     try
     {
         var origin = new City(track[0].ToString());
         var destination = new City(track[1].ToString());
         var distance = Convert.ToInt32(track.Substring(2, track.Length - 2));
         return new Track(origin, destination, distance);
     }
     catch (Exception e)
     {
         throw new FormatException("The input route is incorrect.", e);
     }
 }
コード例 #7
0
        private void FindAllPossibleRoutes(List<Track> railRoad, City origin, RouteWithDistanceFromStart runningRoute)
        {
            var possibleTracks = railRoad.FindAll(track => track.Origin.Equals(origin));
            foreach (var track in possibleTracks)
            {
                var route = (RouteWithDistanceFromStart)runningRoute.Clone();
                route.AppendCity(track.Destination, track.Distance);

                if (ThisTrackWillLeadMeToDestination(track))
                    possibleRoutes.Add(route);

                if (ShouldKeepRunningInThisRoute(runningRoute, track))
                    FindAllPossibleRoutes(railRoad, track.Destination, route);
            }
        }
コード例 #8
0
ファイル: RouteFinder.cs プロジェクト: blackbnu/Trains
        private void FindNumberOfPossibleRoutes(List<Track> railRoad, City origin, Route runningRoute)
        {
            var possibleTracks = railRoad.FindAll(track => track.Origin.Equals(origin));
            foreach (var track in possibleTracks)
            {
                var route = (Route)runningRoute.Clone();
                route.AppendCity(track.Destination);

                if (ThisRouteMatchWithTheRequisites(route))
                    possibleRoutesCounter++;

                if (ShouldKeepRunningInThisRoute(route))
                    FindNumberOfPossibleRoutes(railRoad, track.Destination, route);
            }
        }
コード例 #9
0
        public void Should_Find_Number_Of_Routes_With_Limited_Distances_On_A_Simple_RailRoad()
        {
            var A = new City("A");
            var B = new City("B");
            var railRoad = new List<Track>();
            railRoad.Add(new Track(A, B, 5));
            railRoad.Add(new Track(A, B, 8));
            railRoad.Add(new Track(A, B, 2));
            railRoad.Add(new Track(A, B, 50));
            railRoad.Add(new Track(A, B, 21));
            railRoad.Add(new Track(A, B, 1));
            railRoad.Add(new Track(A, B, 17));
            railRoad.Add(new Track(A, B, 6));

            routesFinder.GetNumberOfRoutesWithDistanceLimit(railRoad, A, B, 6).Should().Be(3);
        }
コード例 #10
0
ファイル: Track.cs プロジェクト: blackbnu/Trains
 public Track(City origin, City destination, int distance)
 {
     Origin = origin;
     Destination = destination;
     Distance = distance;
 }
コード例 #11
0
ファイル: Track.cs プロジェクト: blackbnu/Trains
 public bool IsAConnectionBetween(City origin, City destination)
 {
     return Origin.Equals(origin) && Destination.Equals(destination);
 }
コード例 #12
0
ファイル: DistanceEvaluator.cs プロジェクト: blackbnu/Trains
 private void EvaluateOriginAndDestination(Route route)
 {
     currentCity = route.Cities.Peek();
     route.Cities.Dequeue();
     nextCity = route.Cities.Peek();
 }
コード例 #13
0
 public virtual int Find(List<Track> railroad, City origin, City finaldestination, int maxStops)
 {
     maximumStops = maxStops;
     return FindNumberOfPossibleRoutes(railroad, origin, finaldestination);
 }
コード例 #14
0
 private static RouteWithDistanceFromStart StartRoute(City origin)
 {
     var initialRoute = new RouteWithDistanceFromStart();
     initialRoute.AppendCity(origin, 0);
     return initialRoute;
 }
コード例 #15
0
ファイル: RouteFinder.cs プロジェクト: blackbnu/Trains
 private static Route StartRoute(City origin)
 {
     var initialRoute = new Route();
     initialRoute.AppendCity(origin);
     return initialRoute;
 }
コード例 #16
0
 public virtual int FindPossibleRoutes(List<Track> railRoad, City origin, City finalDestionation, int exactNumberOfStops)
 {
     exactNumberOfStopsExpected = exactNumberOfStops;
     return FindNumberOfPossibleRoutes(railRoad, origin, finalDestionation);
 }
コード例 #17
0
 public void Should_Return_Zero_Routes_If_The_Origin_Is_A_Leaf()
 {
     var railRoad = GivenA.SimpleRailRoad();
     var leaf = new City("Z");
     routeFinder.FindPossibleRoutes(railRoad, leaf, new City("A"), 1).Should().Be(0);
 }
コード例 #18
0
 public void AppendCity(City city, int distance)
 {
     DistanceFromStart += distance;
     Cities.Enqueue(city);
 }