Я так близок к завершению этого проекта, но итератор действительно ставит меня в тупик, так как он относится к MinPQ
и алгоритму A *.Любые рекомендации или советы о том, как я могу лучше реализовать этот метод:
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.Stack;
/**
* Solver class is used to solve any puzzle that is in the "8 puzzle format" It
* implements the A* algorithm which is used in path-finding games and map
* traversals. It approximates the shortest path from start to finish.
*
* @author amie3
*/
public class Solver {
private SearchNode search;// create a field to implement the SearchNode
private int movesToSolve;// field used to find the number of moves it takes to solve our initial puzzle
private boolean canUSolve; // we will initially set it to false in the Solver method
private Object solution;
/**
* The solver method used the A* algorithm to find a solution to the initial
* board using the MinPQ data type
*
* Generic min priority queue implementation with a binary heap. Can be used
* with a comparator instead of the natural order. The MinPQ class represents a
* priority queue of generic keys. It supports the usual insert and
* delete-the-minimum operations, along with methods for peeking at the minimum
* key, testing if the priority queue is empty, and iterating through the keys.
* This implementation uses a binary heap. The insert and delete-the-minimum
* operations take logarithmic amortized time. The min, size, and is-empty
* operations take constant time. Construction takes time proportional to the
* specified capacity or the number of items used to initialize the data
* structure.
*
* Now, we describe a solution to the problem that illustrates a general
* artificial intelligence methodology known as the A* search algorithm. We
* define a search node of the game to be a board, the number of moves made to
* reach the board, and the predecessor search node. First, insert the initial
* search node (the initial board, 0 moves, and a null predecessor search node)
* into a priority queue. Then, delete from the priority queue the search node
* with the minimum priority, and insert onto the priority queue all neighboring
* search nodes (those that can be reached in one move from the dequeued search
* node). Repeat this procedure until the search node dequeued corresponds to a
* goal board. The success of this approach hinges on the choice of priority
* function for a search node. We consider two priority functions:
*
* @param initial
*/
public Solver(Board initial) {
// corner exception, throw illegal argument if the initial board cannot be
// solved
if (initial == null) {
throw new java.lang.IllegalArgumentException("sorry, initial board is not solvable :(");
}
canUSolve = false; // Initialize with false
movesToSolve = 0; // initialize with 0
MinPQ<SearchNode> mini = new MinPQ<SearchNode>(); // create a new instance of the MinPQ class
// First, insert the initial search node (the initial board, 0 moves, and a null
// previous search node) into a priority queue.
mini.insert(new SearchNode(initial, movesToSolve, null));
// if the board is not solved, go through the instructions within the while loop
while (!canUSolve) {
SearchNode lastNode = mini.delMin();// delete from the priority queue the search node with the minimum priority
if (lastNode.board.isGoal()) {// if this is the goal board
movesToSolve = -1;
canUSolve = false;
}
}
}
/**
* Moves method solves the minimum number of moves to solve the initial puzzle
* @return the number of moves it takes to solve
*/
public int moves() {
return movesToSolve;
}
/*
* create a search node for the game to be a board, the number of moves made to
* reach the board and the predecessor search node
*/
private class SearchNode implements Comparable<SearchNode> {
private SearchNode previous;
private Board board;
private int priority; // create an instance of board
public Board board;
// constructor for the search node to implement First, insert the initial search
// node
// (the initial board, 0 moves, and a null previous search node) into a priority
// queue.
public SearchNode(Board initial, int movesToSolve, Object object) {
this.board = board;
this.priority = board.manhattan() + movesToSolve;
}
@Override
public int compareTo(SearchNode other) {
// TODO Auto-generated method stub
return this.priority - other.priority;
}
}
/**
* This methods allows you to able to get an instance of an Iterator, which is
* the needed instance used by the "foreach" loop in our main method
*
* @return
*/
public Iterable<Board> solution() {
return null;
}
}