Java Algorithms Solve Towers of Hanoi puzzle

Description

Java Algorithms Solve Towers of Hanoi puzzle


public class Main {
   static int nDisks = 3;

   public static void main(String[] args) {
      doTowers(nDisks, 'A', 'B', 'C');
   }//from ww w.  jav  a2s .c  o m

   public static void doTowers(int topN, char src, char inter, char dest) {
      if (topN == 1)
         System.out.println("Disk 1 from " + src + " to " + dest);
      else {
         doTowers(topN - 1, src, dest, inter); // src to inter

         System.out.println("Disk " + topN + // move bottom
               " from " + src + " to " + dest);
         doTowers(topN - 1, inter, src, dest); // inter to dest
      }
   }

}



PreviousNext

Related