public class TestHanoi {
public static void main(String[] args) {
hanoi(3,'A','B','C');
}
/**
*
* @param n 有几个盘子
* @param from 开始位置
* @param in 中间位子
* @param to 目标位置
*/
public static void hanoi(int n,char from,char in, char to) {
if(n==1) {
System.out.println("第1个盘子从"+from+"移到"+to);
//无论有多少个盘子,都认为只有两。上面的所有盘子和最下面一个盘子
}else {
//移动上面的所有盘子到中间位子
hanoi(n-1,from,to,in);
//移动下面的盘子
System.out.println("第"+n+"个盘子从"+from+"移到"+to);
//把上面的所有盘子从中间位置移到
hanoi(n-1,in,from,to);
}
}
}