Beispiel #1
0
        // 동적 할당을 해줍니다.
        public BoardNode Find(BoardGame game)
        {
            BoardNode  answer;
            List <Key> keys = new List <Key> {
                Key.Up, Key.Down, Key.Left, Key.Right
            };
            List <Key> reverse = new List <Key> {
                Key.Down, Key.Up, Key.Right, Key.Left
            };

            BoardNode first = new BoardNode(game, new List <Key>(), Board.CheckWrongTiles(game));

            Enqueue(first);

            while (Tree.Count > 1)
            {
                BoardNode pop = Dequeue();
                if (Board.IsSolved(pop.board))
                {
                    answer = BoardNode.Copy(pop);
                    return(answer);
                }

                for (int i = 0; i < 4; i++)
                {
                    if (pop.path.Count > 0)
                    {
                        if (pop.path[pop.path.Count - 1] == reverse[i])
                        {
                            continue;
                        }
                    }
                    if (Board.IsMovable(pop.board, keys[i]) == true)
                    {
                        BoardNode moved = BoardNode.Copy(pop);
                        Board.MoveTileOnly(moved.board, keys[i]);
                        moved.path.Add(keys[i]);
                        moved.heur = moved.path.Count + Board.CheckWrongTiles(moved.board);
                        Enqueue(moved);
                    }
                }
            }
            return(new BoardNode(null, null, -1));
        }
Beispiel #2
0
        public BoardNode Dequeue()
        {
            int       size = Tree.Count();
            BoardNode tmp;

            if (size < 2)
            {
                return(new BoardNode(null, null, -1));
            }

            tmp = BoardNode.Copy(Tree[1]);

            if (size == 2)
            {
                Tree.RemoveAt(1);
                return(tmp);
            }

            // 맨 뒤의 노드 끌어오기
            Tree[1] = Tree[size - 1];
            Tree.RemoveAt(size - 1);
            size--;


            int index = 1;

            while (true)
            {
                if (index * 2 + 1 > size)
                {
                    return(tmp);
                }

                else if (index * 2 + 2 <= size)
                {
                    int       smaller     = (Tree[index * 2].heur > Tree[index * 2 + 1].heur) ? (index * 2 + 1) : (index * 2);
                    BoardNode smallerNode = Tree[smaller];

                    if (Tree[index].heur >= Tree[smaller].heur)
                    {
                        Swap(Tree, index, smaller);
                    }
                    else
                    {
                        return(tmp);
                    }
                    index = smaller;
                }
                else // if (index * 2 + 1 == size)
                {
                    if (Tree[index].heur >= Tree[index * 2].heur)
                    {
                        Swap(Tree, index, index * 2);
                    }
                    else
                    {
                        return(tmp);
                    }

                    index *= 2;
                }
            }
        }