示例#1
0
        private static int GetSomeDistance(Place start, Place current, Place end, Func<IEnumerable<Connection>, Func<Connection, int>, IOrderedEnumerable<Connection>> orderby, int distance = 0, List<string> visited = null)
        {
            if (visited == null)
                visited = new List<string> { start.Name };
            else
                visited.Add(current.Name);

            if (!current.Connections.Any(x => !visited.Contains(x.Destination.Name) && x.Destination.Name != end.Name))
                return distance + current.Connections.Single(x => x.Destination.Name == end.Name).Distance;

            var connection = orderby.Invoke(current.Connections,x => x.Distance).First(x => !visited.Contains(x.Destination.Name) && x.Destination.Name != end.Name);
            return distance + GetSomeDistance(start, connection.Destination, end, orderby, connection.Distance, visited);
        }
示例#2
0
 private static int GetShortestDistance(Place start, Place current, Place end, int distance = 0, List<string> visited = null)
 {
     return GetSomeDistance(start, start, end, Enumerable.OrderBy);
 }
示例#3
0
        public static Place GetOrAddPlace(this ICollection<Place> places, string name)
        {
            var place = places.FirstOrDefault(x => x.Name == name);
            if (place != null)
                return place;

            place = new Place { Name = name };
            places.Add(place);
            return place;
        }