A* Search

If we do a best-first search where the priority queue is ordered by h(s) + d(s), the search algorithm is called "A*" ("A-Star").

It can be proven that if h(s) never overestimates a state's distance to the final state, then A* search is optimal.

As mentioned, the number of tiles out of place heuristic for the 8-Puzzle problem does not overestimate.

A* search is easily implemented by changing the priority queue comparator object to:

       new Comparator<Vertex>() {
            public int compare(Vertex v1, Vertex v2) {
                int h1 = ((State) v1).getHeuristic(finalState);
                int d1 = v1.getDistance();
                int h2 = ((State) v2).getHeuristic(finalState);
                int d2 = v2.getDistance();
                return (h1 + d1) - (h2 + d2);
            }
        }