//  Initial state for n=5
//
//	A		B		C
//	|		|		|
//     (_) 1		|		|
//    (___) 2		|		|
//   (_____) 3		|		|
//  (_______) 4		|		|
// (_________) 5	|		|
//-----------------------------------------------
public class Hanoi
{
    /**
     * Prints how to move n-disks in pile "from" to pile "to"
     * with pile "via" available for intermediate storage, such
     * that only one disk is moved at a time and at no time is 
     * a larger disk above a smaller disk (where smaller disks
     * have smaller numbers than larger disks).
     */
    public void printSolution (int n, String from, String to, String via)
    {
	  // base case: only one disk in pile
	  if (n == 1)
	  {
	      System.out.println("Move disk 1 from " + from + " to " + to);
	  }
	  // general case:
	  //  - move pile above largest disk,
	  //  - move largest disk,
	  //  - move pile back on top
   	  else
	  {
	      printSolution(n-1, from, via, to);  // move disks above to alternate (via)
	      System.out.println("Move disk " + n + " from " + from + " to " + to);
	      printSolution(n-1, via, to, from);  // move disks above to target (to)
	  }
    }


    /**
     * Print solution for user.
     */
    public static void main (String[] args)
    {
	int numDisks = 5;
	if (args.length > 0) numDisks = Integer.parseInt(args[0]);

	Hanoi game = new Hanoi();
	game.printSolution(numDisks, "A", "C", "B");
    }
}
