class Hanoi {
void move (String src, String dst, String aux, int n){
if ( n > 1) // more than 1 disc to move
move (src, aux, dst, n - 1); // First move n-1 discs from the
// source to the auxiliary pole.
System.out.println("Move a disc from " + src + " to " + dst);
// Move the lowest disc.
if (n > 1)
move (aux, dst, src, n-1); // Move the discs you have
// previously moved to the
// auxiliary pole to the destination.
}
public static void main (String[] args){
move ("source", "destination", "aux", 8); // A possible way to
// invoke the function
}
}
2007-02-19 23:38:46
·
answer #1
·
answered by Amit Y 5
·
0⤊
0⤋
The problem of Tower of Hanoi is the best example of recursive functions. To understand the problem visit this link.
http://www.cs.cmu.edu/~cburch/survey/recurse/hanoiimpl.html
Here is a sample program in c++.
http://mbalat.googlepages.com/hanoitowerstest.cpp.
2007-02-21 02:30:49
·
answer #2
·
answered by manoj Ransing 3
·
0⤊
0⤋
well foremost your ques is not too clear.r u asking for the recurcive algo or the non-recursive one.
the recurcive one is very simple if u can understand the logic.you can refer to book on data structure by tannenbaum.
2007-02-20 08:03:15
·
answer #3
·
answered by rudro k 1
·
0⤊
0⤋