Java Applets Centre
Tower of Hanoi


Description
This applet displays the solution of the Tower of Hanoi Puzzle. The solution for a Tower of Hanoi with n discs requires 2^n-1 moves.


Code (Recursive Solution)

void solve(int n, int i, int j, int k){
  if(n==1) movedisk(i,k);
  else{
   solve(n-1,i,k,j);
   solve(1,i,j,k);
   solve(n-1,j,i,k);
  }
}


Data Structures and Algorithms
Java Applets Centre


R. Mukundan
Department of Computer Science
University of Canterbury
Private Bag 4800, Christchurch
New Zealand.