Exemplo n.º 1
0
        public static PathNode ShortestPath(BotMap map, BotTerritory source, BotTerritory destination)
        {
            List <PathNode> seenTerr   = new List <PathNode>();
            PathVector      unseenTerr = new PathVector();

            foreach (BotTerritory terr in map.Territories.Values)
            {
                if (terr.ID == source.ID)
                {
                    unseenTerr.Insert(0, new PathNode(terr));
                    unseenTerr.nodes[0].minDistance = 0;
                }
                else
                {
                    unseenTerr.Add(new PathNode(terr));
                }
            }

            while (unseenTerr.Contains(destination.ID))
            {
                PathNode pointer = unseenTerr.nodes[0];
                foreach (TerritoryIDType terrId in pointer.adjacent)
                {
                    int numArmies = map.Territories[terrId].Armies.NumArmies;
                    if (numArmies == 0)
                    {
                        numArmies = 4;
                    }
                    if (unseenTerr.Contains(terrId) && unseenTerr.GetNode(terrId).minDistance > (pointer.minDistance + numArmies))
                    {
                        PathNode temp = unseenTerr.GetNode(terrId);
                        temp.minDistance = pointer.minDistance + numArmies;
                        temp.minPath     = new List <TerritoryIDType>(pointer.minPath);
                        temp.minPath.Add(pointer.territory);
                    }
                }
                seenTerr.Add(unseenTerr.Remove(pointer.territory));
                Quicksort.QuicksortPath(ref unseenTerr, 0, unseenTerr.nodes.Count);
            }

            return(seenTerr.Last());
        }
Exemplo n.º 2
0
        public static void QuicksortPath(ref PathVector vector, int from, int to)
        {
            if (to - from < 2 || vector == null)
            {
                return;
            }
            int pointer = from;

            for (int i = from + 1; i < to; i++)
            {
                if (vector.nodes[from].minDistance > vector.nodes[i].minDistance)
                {
                    Swap(++pointer, i, ref vector.nodes);
                }
            }

            Swap(pointer, from, ref vector.nodes);
            QuicksortPath(ref vector, from, pointer);
            QuicksortPath(ref vector, pointer + 1, to);
        }