示例#1
0
        /// <summary>
        /// Calculates the shortest route to all the other locations
        /// </summary>
        /// <param name="_startLocation"></param>
        /// <returns>List of all locations and their shortest route</returns>
        public Dictionary<Location, Route> CalculateMinCost(Location _startLocation)
        {
            //Initialise a new empty route list
            Dictionary<Location, Route> _shortestPaths = new Dictionary<Location, Route>();
            //Initialise a new empty handled locations list
            List<Location> _handledLocations = new List<Location>();

            //Initialise the new routes. the constructor will set the route weight to in.max
            foreach (Location location in _locations)
            {
                _shortestPaths.Add(location, new Route(location.Identifier));
            }

            //The startPosition has a weight 0.
            _shortestPaths[_startLocation].Cost = 0;

            //If all locations are handled, stop the engine and return the result
            while (_handledLocations.Count != _locations.Count)
            {
                //Order the locations
                List<Location> _shortestLocations = (List < Location > )(from s in _shortestPaths
                                                        orderby s.Value.Cost
                                                        select s.Key).ToList();

                Location _locationToProcess = null;

                //Search for the nearest location that isn't handled
                foreach (Location _location in _shortestLocations)
                {
                    if (!_handledLocations.Contains(_location))
                    {
                        //If the cost equals int.max, there are no more possible connections to the remaining locations
                        if (_shortestPaths[_location].Cost == int.MaxValue)
                            return _shortestPaths;
                        _locationToProcess = _location;
                        break;
                    }
                }

                //Select all connections where the startposition is the location to Process
                var _selectedConnections = from c in _connections
                                           where c.A == _locationToProcess
                                           select c;

                //Iterate through all connections and search for a connection which is shorter
                foreach (Connection conn in _selectedConnections)
                {
                    if (_shortestPaths[conn.B].Cost > conn.Weight + _shortestPaths[conn.A].Cost)
                    {
                        _shortestPaths[conn.B].Connections = _shortestPaths[conn.A].Connections.ToList();
                        _shortestPaths[conn.B].Connections.Add(conn);
                        _shortestPaths[conn.B].Cost = conn.Weight + _shortestPaths[conn.A].Cost;
                    }
                }
                //Add the location to the list of processed locations
                _handledLocations.Add(_locationToProcess);
            }

            return _shortestPaths;
        }
示例#2
0
文件: Table.cs 项目: hujtomi/games
        public Table()
        {
            IsBallSelected = false;
            SelectedPoint = new Point(-1, -1);

            gametable = new short[tableWidth, tableHeight];

            for (int i = 0; i < 3; )
            {
                Random rand = new Random();

                int x = rand.Next(tableWidth);
                int y = rand.Next(tableHeight);

                int value = rand.Next(7);
                ++value;

                if (gametable[x, y] == 0)
                {
                    gametable[x, y] = (short)value;
                    ++i;
                }
            }

            locations = new Location[tableWidth, tableHeight];
            engine = new RouteEngine.RouteEngine();

            for (int i = 0; i < tableWidth; i++)
            {
                for (int j = 0; j < tableHeight; j++)
                {
                    locations[i, j] = new Location() { Identifier = i + "," + j };
                    engine.Locations.Add(locations[i, j]);
                }
            }
        }
 public Connection(Location a, Location b, double weight)
 {
     this._a = a;
     this._b = b;
     this._weight = weight;
 }