RushHour 状態を表すために使用される、文字列の hashSet があります。hashSet には、以前にアクセスした状態がすべて含まれています。
関数を呼び出して、特定の状態から利用可能な次の移動を返し、コードを使用してそれらが既にアクセスされているかどうかを確認します。
if(!(visitedHash.contains(childBoards.get(i).Convert())))
次に、その状態が訪問されていない場合は、それをキューに追加して、幅優先探索を使用して解決します。
問題は、次のコード行を書くときです。
if(visitedHash.contains(currentBoard.Convert())){
System.out.println("Whats Going on!!??");
}
新しいボードをポーリングすると、多くの状態で「What's Going on !!??」と表示されます。
これは不可能です。それはすべきですか?それらが含まれているかどうかを確認したところ、それらがキューに追加されたので、IFステートメントに合格したに違いありません!
検索方法の完全なコードは次のとおりです。
public void search(Board b){
//--------Perform Breadth First Search on Board b--------//Method to solve the puzzle using Breadth First Search
System.out.println("Attempting to solve the Board using Breadth First Search...");
long startTime = System.nanoTime(); //capture the start time for the method
queue.add(b); //push the input board onto the front of the queue
while(!solved){ //while the board is not solved
currentBoard = queue.poll(); //assign the top of the queue to currentBoard
currentBoard.print(); //print the board to the screen (PRINT EVERY BOARD VISITED)
System.out.println(currentBoard.Convert());
System.out.println("This board is on level " + getLevel(currentBoard));
//System.out.println("Visited Boards size: " + visitedHash.size());
if(visitedHash.contains(currentBoard.Convert())){
System.out.println("Whats Going on!!??");
}
boardsExplored++; //increment the number of boardsExplored
if(currentBoard.isGoal()){ //if the board is the goal state
long endTime = System.nanoTime(); //capture the end time for the method
long duration = endTime - startTime; //calculate the duration for the method
time = (double)duration / 1000000000.0; //convert the time to seconds
System.out.println("SOLVED! Goal car is Free!");
System.out.println("Time taken to solve = " + time + " seconds"); //print the time taken in seconds
System.out.println("Moves made = " + (boardsExplored - 1)); //print the number of boards explored to reach goal
visitedHash.add(currentBoard.Convert()); //add the board (when converted to a string) to the list of visited boards
printSolution(currentBoard); //Call method to print shortest path found
write(b); //Call method to write data to the file
solved = true; //set solved to true
return; //exit the loop
}
visitedHash.add(currentBoard.Convert()); //add the board (when converted to a string) to the list of visited boards
childBoards = currentBoard.getChildMoves(); //call getChildMoves on the currentBoard to retrieve all available boards
for (int i = 0 ; i < childBoards.size() ; i ++){ //for every one of the child boards
if(!(visitedHash.contains(childBoards.get(i).Convert()))){ //if the child board has NOT previously been visited
queue.add(childBoards.get(i)); //add the child board to the queue and loop back
parent.put(childBoards.get(i), currentBoard); //Map the child board to its parent
level.put(childBoards.get(i), getLevel(currentBoard));
}
}
}
}