예제 #1
0
        public static List <T> YEETSort(List <T> yeet, Comparer <T> comparer)
        {
            HeapTree <T> tree = new HeapTree <T>(comparer);

            for (int i = 0; i < yeet.Count; i++)
            {
                tree.Insert(yeet[i]);
            }
            List <T> yeeter = new List <T>();

            for (int i = 0; i < yeet.Count; i++)
            {
                yeeter.Add(tree.Pop());
            }
            return(yeeter);
        }
        public static Stack <Game26> SolvePuzzle(Game26 start, int[,] end)
        {
            Dictionary <int, Point> map = new Dictionary <int, Point>();

            for (int i = 0; i < start.gridSizeX * start.gridSizeY; i++)
            {
                map[i + 1] = new Point((i % start.gridSizeX), (i / start.gridSizeY));
            }

            Game26[] visited = new Game26[10];
            int      index   = 0;

            Comparison <Game26> comparison = new Comparison <Game26>((x, y) => x.finalDistance.CompareTo(y.finalDistance));
            HeapTree <Game26>   heap       = new HeapTree <Game26>(Comparer <Game26> .Create(comparison));

            //Initialize:
            start.reset();
            start.distance      = 0;
            start.finalDistance = heuristic(start, map);
            heap.Insert(start);

            Game26 endGame = null;

            while (heap.Count > 0)
            {
                var current = heap.Pop();

                visited[index] = current;
                index++;
                index %= visited.Length;

                if (AreEqual(current, end))
                {
                    endGame = current;
                    break;
                }

                List <Game26> neighbors = GetNeighbors(current);

                for (int i = 0; i < neighbors.Count; i++)
                {
                    //final step to make this work is figure out a different condition on when to set founder;
                    double tentative = current.distance + 1;

                    if (tentative < neighbors[i].distance)
                    {
                        neighbors[i].distance      = tentative;
                        neighbors[i].founder       = current;
                        neighbors[i].finalDistance = neighbors[i].distance + heuristic(neighbors[i], map);
                    }

                    bool iscontained = false;
                    foreach (var game in visited)
                    {
                        if (game == null)
                        {
                            continue;
                        }

                        if (AreEqual(game, neighbors[i].grid))
                        {
                            iscontained = true;
                        }
                    }

                    if (!iscontained && !heap.Contains(neighbors[i], Game26Equal))
                    {
                        heap.Insert(neighbors[i]);
                    }
                }
            }

            Stack <Game26> result = new Stack <Game26>();

            while (endGame != null)
            {
                result.Push(endGame);
                endGame = endGame.founder;
            }

            return(result);
        }