Exemple #1
0
        public override void Execute(string endstate)
        {
            startTime = DateTime.Now;

            var closetSet = new Dictionary <string, HanoiState>();
            var openSet   = new Dictionary <string, HanoiState>();
            var fScore    = new Dictionary <string, int>();
            var gScore    = new Dictionary <string, int>();
            var cameFrom  = new Dictionary <string, string>();
            var idToState = new Dictionary <string, HanoiState>();

            hanoiState = new HanoiState(this.towers);
            var id = hanoiState.Id;

            openSet[id]   = new HanoiState(hanoiState);
            idToState[id] = openSet[id];
            fScore[id]    = 0;
            gScore[id]    = 0;

            while (openSet.Count() > 0)
            {
                var current = GetLeastCost(openSet, fScore);

                if (current == endstate)
                {
                    result  = ReconstructPath(current, cameFrom);
                    endTime = DateTime.Now;

                    return;
                }

                openSet.Remove(current);
                closetSet[current] = idToState[current];
                hanoiState         = idToState[current];

                var neighbors = hanoiState.GetNeighbor();

                foreach (var neighbor in neighbors)
                {
                    var neighborId = neighbor.Id;

                    if (closetSet.ContainsKey(neighborId))
                    {
                        continue;
                    }

                    var tentativeGScore   = gScore[current] + 1;
                    var tentativeIsBetter = false;
                    if (!openSet.ContainsKey(neighborId))
                    {
                        openSet[neighborId]   = neighbor;
                        idToState[neighborId] = neighbor;
                        counter++;
                        tentativeIsBetter = true;
                    }
                    else if (tentativeGScore < gScore[neighborId])
                    {
                        tentativeIsBetter = true;
                    }

                    if (tentativeIsBetter)
                    {
                        cameFrom[neighborId] = current;
                        gScore[neighborId]   = tentativeGScore;
                        fScore[neighborId]   = tentativeGScore;
                    }
                }
            }
        }