title: Modifying this 8-puzzle code to print the intermediate states to reach the solution
body: <p><a href="http://www.8puzzle.com/8_puzzle_algorithm.html" rel="nofollow noreferrer">About the 8 Puzzle Problem</a></p>
  
  <p>// Breadth First Search Usage in the common Eight Puzzle Problem.</p>
  
  <pre><code>import java.util.*;
  
  class EightPuzzle {
  
      Queue&lt;String&gt; q = new LinkedList&lt;String&gt;();    // Use of Queue Implemented using LinkedList for Storing All the Nodes in BFS.
      Map&lt;String,Integer&gt; map = new HashMap&lt;String, Integer&gt;(); // HashMap is used to ignore repeated nodes
  
      public static void main(String args[]){
  
  
          String str="087465132";                 // Input the Board State as a String with 0 as the Blank Space
  
          EightPuzzle e = new EightPuzzle();      // New Instance of the EightPuzzle
          e.add(str,0);                           // Add the Initial State
  
          while(e.q.peek()!=null){
  
              e.up(e.q.peek());                   // Move the blank space up and add new state to queue
              e.down(e.q.peek());                 // Move the blank space down
              e.left(e.q.peek());                 // Move left
              e.right(e.q.remove());              // Move right and remove the current node from Queue
          }
          System.out.println("Solution doesn't exist");
      }
  
      //Add method to add the new string to the Map and Queue
      void add(String str,int n){
          if(!map.containsKey(str)){
              map.put(str,n);
              q.add(str);
          }
      }
  
      /* Each of the Methods below Takes the Current State of Board as String. Then the operation to move the blank space is done if possible.
            After that the new string is added to the map and queue.If it is the Goal State then the Program Terminates.
      */
      void up(String str){
          int a = str.indexOf("0");
          if(a&gt;2){
              String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt(a-3)+str.substring(a+1);
              add(s,map.get(str)+1);
              if(s.equals("123456780")) {
                  System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
                  System.exit(0);
              }
          }
      }
      void down(String str){
          int a = str.indexOf("0");
          if(a&lt;6){
              String s = str.substring(0,a)+str.substring(a+3,a+4)+str.substring(a+1,a+3)+"0"+str.substring(a+4);
              add(s,map.get(str)+1);
              if(s.equals("123456780")) {
                  System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
                  System.exit(0);
              }
          }
      }
      void left(String str){
          int a = str.indexOf("0");
          if(a!=0 &amp;&amp; a!=3 &amp;&amp; a!=6){
              String s = str.substring(0,a-1)+"0"+str.charAt(a-1)+str.substring(a+1);
              add(s,map.get(str)+1);
              if(s.equals("123456780")) {
                  System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
                  System.exit(0);
              }
          }
      }
      void right(String str){
          int a = str.indexOf("0");
          if(a!=2 &amp;&amp; a!=5 &amp;&amp; a!=8){
              String s = str.substring(0,a)+str.charAt(a+1)+"0"+str.substring(a+2);
              add(s,map.get(str)+1);
              if(s.equals("123456780")) {
                  System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
                  System.exit(0);
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to modify the code so that it prints the intermediate states used to reach the solution, instead of just saying the level on which the solution was reached.</p>
  
  <p>For example, given this board</p>
  
  <pre><code>1 4 2
  3 0 5
  6 7 8
  </code></pre>
  
  <p>(as String 142305678)</p>
  
  <p>I want it to print:</p>
  
  <pre><code>1 4 2
  3 0 5
  6 7 8
  </code></pre>
  
  <p>(as the String 142305678)</p>
  
  <pre><code>1 0 2
  3 4 5
  6 7 8
  </code></pre>
  
  <p>(as the String 102345678)</p>
  
  <pre><code>0 1 2
  3 4 5
  6 7 8
  </code></pre>
  
  <p>(as the String 012345678)</p>
  
  <p>By looking at the code, I believe this intermediate strings are getting stored via the add method into the Queue:</p>
  
  <pre><code>void add(String str,int n){
          if(!map.containsKey(str)){
              map.put(str,n);
              q.add(str);
          }
      }
  </code></pre>
  
  <p>I have no experience working with HashMap, how would I look into the intermediate states stored there? </p>