The code copy is as follows:
package com.tiantian.algorithms;
/**
* _|_1 | |
* __|__2 | |
* ___|___3 | | (1). Move the 4 blocks on A to C.
* _______4 | |
* ABC
*
* | | |
* | _|_1 |
* | __|__2 | To complete the effect of (1), you must move the 1, 2, and 3 wood blocks to B so that you can move 4 to C
* _______4 ____|___3 | For example: "Call (XX)" in the code
* ABC
*
* | | |
* | _|_1 |
* | __|__2 | At this time, the question becomes to move the 3 wooden blocks on B to C, and return to the question (1)
* | ___|___3 _____|____4 For example: "Call (YY)" in the code
* ABC
*
* Then loop through this process
*
* @author wangjie
* @version Created at: 2013-3-4 4:09:53 pm
*/
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("Finally transfer the wood block 1 from" + from + "Move to" + to);
}else{
doTowers(topN - 1, from, to, inter); // Call (XX)
System.out.println("Take the wood block" + topN + "From" + from + "Move to" + to);
doTowers(topN - 1, inter, from ,to); // Call (YY)
}
}
}