title: A* Algorithm 8-puzzle
body: <p>I have read many pseudo code for the A* algorithm, but neither of them actually explain how to output the solution. I believe I understand the concept of using a priority queue for the not yet visited and a has table for the explored, but when I go through the algorithm I don't know at what point to print out the result. Does any one have a pseudo code that actually show how to output the path?</p>
  
  <p>Id really appreciate it. I have been trying to use the algorithm to implement the 8-puzzle problem</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class Board{
  
  private int[][] squares;
  private int f;
  private int g;
  private int h;
  
  private int size;
  private Board parent;
  
  public Board(Board current, Board parent)
  {
      this(current);
      g = current.getG();
      h = current.getH();
      f = current.getF();
      this.parent = parent;
  
  }
  
  public void solveH1()
  {
  
      while(!frontier.isEmpty())
      {
          board = frontier.poll();
  
          ArrayList&lt;Board&gt; successors = new ArrayList&lt;Board&gt;();
          Board b1 = new Board(board.moveDown(),board);
          Board b2 = new Board(board.moveUp(),board);
          Board b3 = new Board(board.moveLeft(),board);
          Board b4 = new Board(board.moveRight(),board);
          if(!b1.equals(board))
              successors.add(b1);
          if(!b2.equals(board))
              successors.add(b2);
          if(!b3.equals(board))
              successors.add(b3);
          if(!b4.equals(board))
              successors.add(b4);
          for(int i=0; i&lt;successors.size(); i++)
          {
              if(successors.get(i).isGoal())
              {
                  break;
              }
              int g = board.getG()+1;
              int h = successors.get(i).getH1Cost();
              successors.get(i).setG(g);
              successors.get(i).setH(h);
              successors.get(i).setF(g+h);
  
              if(frontier.contains(successors.get(i)))
              {
                  Iterator&lt;Board&gt; iterator = frontier.iterator();
                  Board b = null;
                  while(iterator.hasNext())
                  {
                      b = iterator.next();
                      if(b.equals(successors.get(i)))
                      {
                          break;
                      }
                  }
                  if(b.getG() &lt; successors.get(i).getG())
                  {
                      break;
                  }
              }
              if(exploredSet.contains(successors.get(i)))
              {
                  int index = exploredSet.indexOf(successors.get(i));
                  if(exploredSet.get(index).getG() &lt; successors.get(i).getG())
                      break;
              }
              else
              {
                  frontier.add(successors.get(i));
              }
          }
          exploredSet.add(board);
      }
      printPath();
  }
  public void printPath()
  {
  
      ArrayList&lt;Board&gt; path = new ArrayList&lt;Board&gt;();
      cursor = board;
      while(cursor.getParent()!=null)
      {
          path.add(cursor);
          cursor = cursor.getParent();
      }
      for(int i=0; i&lt;path.size(); i++)
          System.out.println(path.get(i));
  }
  </code></pre>
  
  <p>for some reason, this just prints one node, and it's bot even the goal. Can anyone tell me what I am missing?</p>