package suanfa;
public class HanoiTower {
public static void main(String[] args) {
int n = 3;
hanoTower(n,'A','B','C');
}
private static void hanoTower(int n, char from, char in, char to) {
if(n==1) {
System.out.println("把第"+n+"个盘子从"+from+"移到"+to);
}else{
//先把n-1个移到B
hanoTower(n-1,from,to,in);
//然后将第n个盘子移到C
hanoTower(1,from,in,to);
//再将B上的盘子移到C上
hanoTower(n-1,in,from,to);
}
}
}
总结:其实就是两步走,第一步将n-1个盘子上面的盘子移到中介柱子上,然后将最后一个盘移到目标柱子上,最后一步就是将n-1盘子移到目标柱子上