Пример #1
0
        public void Search(RubickMatrix _start)
        {
            result = null;
            closedSet.Clear();
            openSet.Clear();
            openSet.Add(_start);

            gScore.Clear();
            gScore[_start] = 0;

            fScore.Clear();
            fScore[_start] = HeuristicCostEstimate(_start);
            iterations     = 0;
            RubickMatrix current = null;

            while (openSet.Count > 0)
            {
                iterations++;
                current = FindLowestFScore();
                if (current.IsResolved())
                {
                    result = current;
                    return;
                }

                openSet.Remove(current);
                closedSet.Add(current);

                IList <RubickMatrix> neighbors = GenerateNeighbors(current);

                foreach (var neighbor in neighbors)
                {
                    if (closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    int tentativeGScore = GetGScore(current) + DistanceBetween(current, neighbor);
                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                    else if (tentativeGScore >= GetGScore(neighbor))
                    {
                        continue;
                    }

                    gScore[neighbor] = tentativeGScore;
                    fScore[neighbor] = gScore[neighbor] + HeuristicCostEstimate(neighbor);
                }
            }
        }