コード例 #1
0
        public List <Key> Solve()
        {
            PriorityQueue4AStar pq     = new PriorityQueue4AStar();
            BoardNode           answer = pq.Find(this);

            return(answer.path);
        }
コード例 #2
0
ファイル: PriorityQueue4AStar.cs プロジェクト: CYAN4S/Sloyd
        // 동적 할당을 해줍니다.
        public static BoardNode Copy(BoardNode bn)
        {
            BoardNode result = new BoardNode
            {
                board = Board.DeepCopy(bn.board),
                path  = new List <Key>(bn.path),
                heur  = bn.heur
            };

            return(result);
        }
コード例 #3
0
ファイル: PriorityQueue4AStar.cs プロジェクト: CYAN4S/Sloyd
 public void Enqueue(BoardNode node)
 {
     Tree.Add(node);
     for (int i = Tree.Count() - 1; i > 1; i /= 2)
     {
         if (Tree[i / 2].heur > Tree[i].heur)
         {
             Swap(Tree, i / 2, i);
         }
         else
         {
             return;
         }
     }
 }
コード例 #4
0
ファイル: PriorityQueue4AStar.cs プロジェクト: CYAN4S/Sloyd
        // 동적 할당을 해줍니다.
        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));
        }
コード例 #5
0
ファイル: PriorityQueue4AStar.cs プロジェクト: CYAN4S/Sloyd
        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;
                }
            }
        }