汉诺塔问题:
有三根柱子,开始时,所有的圆盘按从大到小的次序自下而上叠放在一根柱子上(圆锥形结构),现在要将整叠圆盘从一根柱子移到另一根柱子。移动时满足:
- 每次只能移动柱子最上面一个圆盘;
- 不能出现大圆盘在小圆盘上方的情况。
将n个圆盘借助B柱从A柱移动到C柱
public static void TowersOfHanoi(int n, char A, char B, char C) {
if (n == 1) {
System.out.println("move disk from " + A + " to " + C);
return;
}
TowersOfHanoi(n-1, A, C, B);
System.out.println("move disk from " + A + " to " + C);
TowersOfHanoi(n-1, B, A, C);
}
来源:CSDN
作者:wooHsi
链接:https://blog.csdn.net/wooHsi/article/details/103465355