Beispiel #1
0
 public Route GetShortestRoute(RicePaddy start, RicePaddy end)
 {
     if (!m_shortestRoutesMap.ContainsKey(start))
     {
         m_shortestRoutesMap[start] = CalculateShortestRoutes(start);
     }
     return(m_shortestRoutesMap[start][end]);
 }
Beispiel #2
0
        Dictionary <RicePaddy, Route> CalculateShortestRoutes(RicePaddy start)
        {
            var shortestRoutes = new Dictionary <RicePaddy, Route>();
            var handled        = new List <RicePaddy>();

            foreach (var ricePaddy in m_ricePaddies)
            {
                shortestRoutes.Add(ricePaddy, new Route(ricePaddy.Identifier));
            }

            shortestRoutes[start].TotalDistance = 0;

            while (handled.Count != m_ricePaddies.Count)
            {
                var shortestRicePaddies = shortestRoutes.OrderBy(_ => _.Value.TotalDistance).Select(_ => _.Key).ToArray();

                var processing = default(RicePaddy);
                foreach (var ricePaddy in shortestRicePaddies)
                {
                    if (!handled.Contains(ricePaddy))
                    {
                        if (shortestRoutes[ricePaddy].TotalDistance == int.MaxValue)
                        {
                            return(shortestRoutes);
                        }
                        processing = ricePaddy;
                        break;
                    }
                }

                var selectedRoads = m_roads.Where(_ => _.A == processing);

                foreach (var road in selectedRoads)
                {
                    if (shortestRoutes[road.B].TotalDistance > road.Distance + shortestRoutes[road.A].TotalDistance)
                    {
                        var roads = shortestRoutes[road.A].Roads.ToList();
                        roads.Add(road);
                        shortestRoutes[road.B].Roads         = roads;
                        shortestRoutes[road.B].TotalDistance = road.Distance + shortestRoutes[road.A].TotalDistance;
                    }
                }
                handled.Add(processing);
            }

            return(shortestRoutes);
        }
Beispiel #3
0
        Dictionary<RicePaddy, Route> CalculateShortestRoutes(RicePaddy start)
        {
            var shortestRoutes = new Dictionary<RicePaddy, Route>();
            var handled = new List<RicePaddy>();

            foreach (var ricePaddy in m_ricePaddies)
            {
                shortestRoutes.Add(ricePaddy, new Route(ricePaddy.Identifier));
            }

            shortestRoutes[start].TotalDistance = 0;

            while (handled.Count != m_ricePaddies.Count)
            {
                var shortestRicePaddies = shortestRoutes.OrderBy(_ => _.Value.TotalDistance).Select(_ => _.Key).ToArray();

                var processing = default(RicePaddy);
                foreach (var ricePaddy in shortestRicePaddies)
                {
                    if (!handled.Contains(ricePaddy))
                    {
                        if (shortestRoutes[ricePaddy].TotalDistance == int.MaxValue)
                            return shortestRoutes;
                        processing = ricePaddy;
                        break;
                    }
                }

                var selectedRoads = m_roads.Where(_ => _.A == processing);

                foreach (var road in selectedRoads)
                {
                    if (shortestRoutes[road.B].TotalDistance > road.Distance + shortestRoutes[road.A].TotalDistance)
                    {
                        var roads = shortestRoutes[road.A].Roads.ToList();
                        roads.Add(road);
                        shortestRoutes[road.B].Roads = roads;
                        shortestRoutes[road.B].TotalDistance = road.Distance + shortestRoutes[road.A].TotalDistance;
                    }
                }
                handled.Add(processing);
            }

            return shortestRoutes;
        }
Beispiel #4
0
        public void Constructor_should_be_initialized_by_non_null_if_number_that_is_not_divisible_by_10_is_passed()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var actualValue = 0;
                PRandom.Next().Body = @this => 9;
                PNullable<int>.ConstructorT().Body = (ref Nullable<int> @this, int value) =>
                {
                    actualValue = value;
                    @this = IndirectionsContext.ExecuteOriginal(() => new Nullable<int>(value));
                };

                
                // Act
                var paddy = new RicePaddy(1, new Random());

                
                // Assert
                Assert.AreEqual(9000, actualValue);
            }
        }
Beispiel #5
0
 public FarmRoad(RicePaddy a, RicePaddy b, int distance)
 {
     A = a;
     B = b;
     Distance = distance;
 }
Beispiel #6
0
 public FarmRoad(RicePaddy a, RicePaddy b, int distance)
 {
     A        = a;
     B        = b;
     Distance = distance;
 }
Beispiel #7
0
 public Route GetShortestRoute(RicePaddy start, RicePaddy end)
 {
     if (!m_shortestRoutesMap.ContainsKey(start))
         m_shortestRoutesMap[start] = CalculateShortestRoutes(start);
     return m_shortestRoutesMap[start][end];
 }