Copy Code code as follows:
Package com.tiantian.algorithms;
/**
* _|_1 | |
* __|__2 | |
* ___|___3 | | (1). Move 4 pieces of wood from A to C.
* ____|____4 | |
* A B C
*
* | | |
* | _|_1 |
* | __|__2 | To achieve the effect (1), you must move the 1, 2, 3 pieces of wood to B, so that you can move 4 to C
* ____|____4 ___|___3 | such as: "Call (XX)" in code
* A B C
*
* | | |
* | _|_1 |
* | __|__2 | At this point, the title becomes the move 3 pieces of wood on B to C, back to the title (1)
* | ___|___3 ____|____4 such as: "Call (YY)" in code
* A B C
*
* and then loop the process
*
* @author Wangjie
* @version Date Created: 2013-3-4 4:09:53
*/
public class Hanoitowertest {
public static void Main (string[] args) {
Dotowers (4, ' A ', ' B ', ' C ');
}
public static void Dotowers (int topn, char from, Char Inter, char to) {
if (topn = = 1) {
System.out.println ("The last block 1 from the" + from + "move to" + to);
}else{
Dotowers (TopN-1, from, to, Inter); Call (XX)
System.out.println ("Put the block" + TOPN + "from" + from + "move to" + to);
Dotowers (TopN-1, Inter, from, to); Call (YY)
}
}
}