0
点赞
收藏
分享

微信扫一扫

模拟与高精度

快乐小鱼儿_9911 2023-11-05 阅读 21

 汉诺塔


  汉诺塔规则

  •  disk表示圆盘数
  • 一次只能移动一个圆盘
  • 小圆盘只能在大圆盘的上方
  • A 、B 、C分别表示圆柱
  • A为起始圆柱、B为中转圆柱、C为终止圆柱

disk = 1 时:

移动次数为:2^1 - 1

只需要将绿色圆盘 A->C 直接移过去;


disk = 2 时:

移动次数为:2^2 - 1,一次只能移动一个圆盘

  1. 黄色圆盘 A->B
  2. 绿色圆盘 A->C
  3. 黄色圆盘B->C


disk = 3 时:

移动次数为:2^3 - 1,一次只能移动一个圆盘

  1. 粉色圆盘A->C
  2. 黄色圆盘A->B
  3. 粉色圆盘C->B
  4. 绿色圆盘A->C
  5. 粉色圆盘B->A
  6. 黄色圆盘B->C
  7. 粉色圆盘A->C


递归分析


 disk = n 时:

移动次数为:2^n - 1,一次只能移动一个圆盘

错误递归分析


错误代码分析

public class TowerOfHanoi {
    public static void hanoi(int n, char posA, char posB, char posC) {
        // hanoi(圆盘个数,参数1,参数2,参数3)
        // 参数1表示起始位置、参数2表示中转位置、参数3表示终止位置
        if(n == 1) {
            move(posA, posC);
            return; // 递归一定要return
        }
        // 这一步就是将 圆柱A 上的 n-1个 圆盘
        // 借助圆柱C 移动到 圆柱B上
        hanoi(n-1, posA, posC, posB);

        // 将圆柱A上剩下的一个移到圆柱C上
        move(posA, posC);

        // 将 圆柱B 上的所有圆盘 借助圆柱C 移到圆柱A上
        hanoi(n-1, posB, posC, posA); // 这里是错的
    }
    public static void move(char pos1, char pos2) {
        System.out.println(pos1 + "->" + pos2);
    }
    public static void main(String[] args) {
        hanoi(3, 'A', 'B', 'C');
    }
}

 正确递归分析

  1.  当desk = n个圆盘时,需要将圆柱A上面的n-1个圆盘,借助圆柱C最终移动到圆柱B上;
  2. 此时圆柱A上就只剩1个圆盘,则直接从圆柱A移动到圆柱C;
  3. 圆柱B上的n-1个圆盘,借助圆柱A最终移动到圆柱C上;
  4. 此时圆柱B上就只剩一个圆盘,则直接从圆柱B移动到圆柱C;

 


正确代码分析

public class TowerOfHanoi {
    public static void hanoi(int n, char posA, char posB, char posC) {
        // hanoi(圆盘个数,参数1,参数2,参数3)
        // 参数1表示起始位置、参数2表示中转位置、参数3表示终止位置
        if(n == 1) {
            move(posA, posC);
            return; // 递归一定要return
        }
        // 这一步就是将 圆柱A 上的 n-1个 圆盘
        // 借助圆柱C 移动到 圆柱B上
        hanoi(n-1, posA, posC, posB);

        // 将圆柱A上剩下的一个移到圆柱C上
        move(posA, posC);
        
        
        hanoi(n-1, posB, posA, posC);
    }
    public static void move(char pos1, char pos2) {
        System.out.println(pos1 + "->" + pos2);
    }
    public static void main(String[] args) {
        hanoi(3, 'A', 'B', 'C');
    }
}
举报

相关推荐

0 条评论