コード例 #1
0
        private EightPuzzleResult Solve(PriorityQueue <AStarState> queue)
        {
            int visitedNodesCount = 0;

            while (true)
            {
                AStarState state = queue.TakeLast();
                visitedNodesCount++;

                if (state.Board.Equals(Board.FinalBoard))
                {
                    return(new EightPuzzleResult(state, visitedNodesCount));
                }

                IReadOnlyCollection <Board> neighborBoards = state.Board.GetNeighbors();

                foreach (Board board in neighborBoards)
                {
                    if (!WasVisitedPreviously(state, board))
                    {
                        AStarState newState = stateFactory.Create(board, state);
                        queue.Add(newState);
                    }
                }
            }
        }
コード例 #2
0
        private EightPuzzleResult Solve(PriorityQueue <AStarState> queue)
        {
            HashSet <int> visitedBoardHashes = new HashSet <int>();
            int           visitedNodesCount  = 0;

            while (true)
            {
                AStarState state = queue.TakeLast();
                visitedNodesCount++;

                if (state.Board.Equals(Board.FinalBoard))
                {
                    return(new EightPuzzleResult(state, visitedNodesCount));
                }

                IReadOnlyCollection <Board> neighborBoards = state.Board.GetNeighbors();

                foreach (Board board in neighborBoards)
                {
                    int boardHash = board.GetHashCode();

                    if (!visitedBoardHashes.Contains(boardHash))
                    {
                        AStarState newState = stateFactory.Create(board, state);
                        queue.Add(newState);
                        visitedBoardHashes.Add(boardHash);
                    }
                }
            }
        }
コード例 #3
0
        public AStarStateWithVisitedHashes Create(Board board, AStarStateWithVisitedHashes previousState)
        {
            AStarState baseState = baseStateFactory.Create(board, previousState);
            var        hashSet   = new HashSet <int>(previousState.VisitedStatesHashes);

            hashSet.Add(board.GetHashCode());
            return(new AStarStateWithVisitedHashes(baseState, hashSet));
        }
コード例 #4
0
        public EightPuzzleResult Solve(Board startBoard)
        {
            AStarState startingState = stateFactory.CreateStartingState(startBoard);

            var queue = new PriorityQueue <AStarState>();

            queue.Add(startingState);

            return(Solve(queue));
        }
コード例 #5
0
        public AStarStateWithVisitedHashes CreateStartingState(Board startingBoard)
        {
            AStarState baseState = baseStateFactory.Create(startingBoard, null);

            return(new AStarStateWithVisitedHashes(baseState, new HashSet <int>(startingBoard.GetHashCode())));
        }
コード例 #6
0
 public AStarStateWithVisitedHashes(AStarState baseState, HashSet <int> visitedStatesHashes)
     : base(baseState.Board, baseState.PreviousState, baseState.CurrentHeuristicScore, baseState.TotalHeuristicScore)
 {
     VisitedStatesHashes = visitedStatesHashes;
 }