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); } } } }
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); } } } }
public AStarStateWithVisitedHashes CreateStartingState(Board startingBoard) { AStarState baseState = baseStateFactory.Create(startingBoard, null); return(new AStarStateWithVisitedHashes(baseState, new HashSet <int>(startingBoard.GetHashCode()))); }