汉诺塔问题1
有三根相邻的柱子,标号为A,B,C,A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘,要把所有盘子一个一个移动到柱子B上,并且每次移动同一根柱子上都不能出现大盘子在小盘子上方;
如图:
思路
1、把1~N-1 个圆盘移动到B柱
2、把N圆盘移动到C柱
如此继续反复拆分子问题
代码实现
public void hannoiTower(int N,String from,String to,String other){
if(N==1){
System.out.println("Move 1 from "+ from + "to" + to );
}else{
hannoiTower(N-1,from,other,to);
System.out.println("Move "+N+" from "+ from + "to" + to);
hannoiTower(N-1,other,to,from);
}
}