分解步骤如下
主要分为三部
- 将A最上面的n-1个圆环移动到C上,B做辅助点
- 将A上最后一个圆环移动到B上
- 将C上的n-1个圆环移动到B上,A做辅助点
public class _汉诺塔递归 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hannota(3, "a", "b", "c");
}
static void Hannota(int N,String from,String to,String help) {
if (N == 1) {
System.out.println(from + "-->" + to);
return;
}else {
Hannota(N-1, from, help, to);
System.out.println(from + "-->" +to);
Hannota(N-1, help, to, from);
}
}
}