Hanoi puzzle : Algorithms « Collections Data Structure « Java






Hanoi puzzle

Hanoi puzzle
 
public class HanoiTower {
  static int nDisks = 3;

  public static void main(String[] args) {
    hanoiTower(nDisks, 'A', 'B', 'C');
  }

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

           
         
  








Related examples in the same category

1.AnagramsAnagrams
2.FibonacciFibonacci
3.Sieve Sieve
4.Find connections using a depth-first searchFind connections using a depth-first search
5.Find connections using hill climbing.
6.Find optimal solution using least-cost
7.Find the lost keysFind the lost keys
8.Compute the area of a triangle using Heron's FormulaCompute the area of a triangle using Heron's Formula
9.Compute prime numbers
10.Print a table of fahrenheit and celsius temperatures 1
11.Print a table of fahrenheit and celsius temperatures 2
12.Print a table of Fahrenheit and Celsius temperatures 3Print a table of Fahrenheit and Celsius temperatures 3
13.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
14.A programmable Finite State Machine implementation.
15.An extendable Graph datastructure.
16.Utilities for flop (floating-point operation) counting.
17.LU Decomposition
18.Reverse Polish Notation
19.Permutator test
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion