汉诺塔问题 (递归)

在这里插入图片描述

思路

在这里插入图片描述

代码实现

package digui;

import java.util.Stack;

public class Code02_Hanoi {
    // 基础班汉诺塔问题
    public static void hanoi1(int n) {
        leftToRight(n);
    }

    // 请把1~N层圆盘 从左 -> 右
    public static void leftToRight(int n) {
        if (n == 1) { // base case
            System.out.println("Move 1 from left to right");
            return;
        }
        leftToMid(n - 1);
        System.out.println("Move " + n + " from left to right");
        midToRight(n - 1);
    }

    // 请把1~N层圆盘 从左 -> 中
    public static void leftToMid(int n) {
        if (n == 1) {
            System.out.println("Move 1 from left to mid");
            return;
        }
        leftToRight(n - 1);
        System.out.println("Move " + n + " from left to mid");
        rightToMid(n - 1);
    }

    public static void rightToMid(int n) {
        if (n == 1) {
            System.out.println("Move 1 from right to mid");
            return;
        }
        rightToLeft(n - 1);
        System.out.println("Move " + n + " from right to mid");
        leftToMid(n - 1);
    }

    public static void midToRight(int n) {
        if (n == 1) {
            System.out.println("Move 1 from mid to right");
            return;
        }
        midToLeft(n - 1);
        System.out.println("Move " + n + " from mid to right");
        leftToRight(n - 1);
    }

    public static void midToLeft(int n) {
        if (n == 1) {
            System.out.println("Move 1 from mid to left");
            return;
        }
        midToRight(n - 1);
            System.out.println("Move " + n + " from mid to left");
        rightToLeft(n - 1);
    }

    public static void rightToLeft(int n) {
        if (n == 1) {
            System.out.println("Move 1 from right to left");
            return;
        }
        rightToMid(n - 1);
        System.out.println("Move " + n + " from right to left");
        midToLeft(n - 1);
    }

    // 进阶递归版
    public static void hanoi2(int n) {
        if (n > 0) {
            func(n, "left", "right", "mid");
        }
    }

    public static void func(int N, String from, String to, String other) {
        if (N == 1) {
            System.out.println("Move 1 from " + from + " to " + to);
        } else {
            func(N-1, from, other, to);
            System.out.println("Move" + N + " from " + from + " to " + to);
            func(N-1, other, to, from);
        }
    }

    // 迭代版(不重要)
    public static class Record {
        public boolean finish1;
        public int base;
        public String from;
        public String to;
        public String other;

        public Record(boolean f1, int b, String f, String t, String o) {
            finish1 = false;
            base = b;
            from = f;
            to = t;
            other = o;
        }
    }

    public static void hanoi3(int N) {
        if (N < 1) {
            return;
        }
        Stack<Record> stack = new Stack<>();
        stack.add(new Record(false, N, "left", "right", "mid"));
        while (!stack.isEmpty()) {
            Record cur = stack.pop();
            if (cur.base == 1) {
                System.out.println("Move 1 from " + cur.from + " to " + cur.to);
                if (!stack.isEmpty()) {
                    stack.peek().finish1 = true;
                }
            } else {
                if (!cur.finish1) {
                    stack.push(cur);
                    stack.push(new Record(false, cur.base - 1, cur.from, cur.other, cur.to));
                } else {
                    System.out.println("Move " + cur.base + " from " + cur.from + " to " + cur.to);
                    stack.push(new Record(false, cur.base - 1, cur.other, cur.to, cur.from));
                }
            }
        }
    }

	// 主函数
    public static void main(String[] args) {
        int n = 4;
        hanoi1(n);
        System.out.println("============");
        hanoi2(n);
		System.out.println("============");
		hanoi3(n);
    }
}
### PTA 汉诺塔问题递归解题思路 对于经典的汉诺塔问题,当给定三个柱子(源柱`a`、辅助柱`b`和目标柱`c`)以及位于源柱上的若干不同大小的圆盘时,目的是将这些圆盘全部按照相同顺序移至目标柱上。此过程中需遵循两条原则:每次仅能移动最上方的一个圆盘;任何时候都不能将较大的圆盘置于较小者之上。 #### 递归算法的核心逻辑在于: - 对于只有一个圆盘的情况,直接将其由源柱移动到目标柱即可完成任务。 - 若存在多个圆盘,则先利用递归方法把除了最大那个以外的所有其他圆盘借助目标柱作为中介转移到辅助柱上去; - 接着单独转移最大的圆盘到达目的地; - 最后再一次采用递归手段把之前暂存于辅助柱的小型圆盘们搬回原位即目标柱处[^2]。 下面是基于上述原理编写的C语言版本的汉诺塔递归解决方案: ```c #include <stdio.h> void move(char from_peg, char to_peg){ printf("Move disk from %c to %c\n",from_peg,to_peg); } // n表示要移动的磁盘数量,a,b,c分别是起始杆,过渡杆,目的杆. void hanoi(int n,char a,char b,char c){ if(n==1){ // 只有一个磁盘的情况下直接从a移动到c move(a,c); }else{ hanoi(n-1,a,c,b); // 把n-1个磁盘从a通过c移动到b move(a,c); // 单独移动第n个磁盘从a到c hanoi(n-1,b,a,c); // 再次调用函数把剩余的n-1个磁盘从b通过a移动到c } } int main(){ int num; scanf("%d",&num); hanoi(num,'A','B','C'); return 0; } ``` 这段代码实现了标准版三柱汉诺塔游戏的模拟过程,并打印出了每一步骤的具体动作描述。其中核心部分是`hanoi()`函数内部定义了一个条件分支结构来区分单件物品传输还是多级嵌套式的复杂情形处理机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值