双色Hanoi塔问题-递归

双色Hanoi塔问题-递归

在这里插入图片描述
思想:1-将n-1个圆盘从A移动到C,B为辅助
2-将第n个圆盘从A移动到B,C为辅助
3-将n-1个圆盘从C移动到B,A为辅助

方式1:键盘输入,控制台输出。


import java.util.Scanner;

public class Hanoi {
    public static void hanoi(int n, String from, String to, String helper){
        if(n == 1){ //递归出口,就1个圆盘
            System.out.println(n + " " + from  + " " + to);
            return ;
        }
        hanoi(n-1, from, helper, to) ; //将n-1个圆盘从A移动到C,B为辅助
        System.out.println(n + " " + from  + " " + to); //将第n个圆盘从A移动到B
        hanoi(n-1, helper, to, from) ; //将n-1个圆盘从C移动到B,A为辅助
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        int n = input.nextInt() ;
        hanoi(n, "A", "B", "C") ;
    }
}

方式2:文件流输入输出。

import java.io.*;

public class Hanoi1 {
    public static String line ;
    public static int n ;
    public static String [] array = new String [10000];
    public static int i = 0 ;
    public static void hanoi(int n, String from, String to, String helper) throws IOException {

        if(n == 1){ //递归出口,就1个圆盘
            array[i++] = n + " " + from  + " " + to ;
            return ;
        }
        hanoi(n-1, from, helper, to) ; //将n-1个圆盘从A移动到C,B为辅助
        array[i++] = n + " " + from  + " " + to ;
        hanoi(n-1, helper, to, from) ; //将n-1个圆盘从C移动到B,A为辅助
    }
    public static void main(String[] args) {
        try (FileInputStream input = new FileInputStream("D:\\study\\input.txt");
             DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(input))){
            line = dataInputStream.readLine() ;
            n = Integer.parseInt(line) ;
            hanoi(n, "A", "B", "C") ;
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        try ( FileOutputStream output = new FileOutputStream("D:\\study\\output.txt");
              DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(output))){
            for(int j=0; j<array.length; j++) {
                if(array[j] != null)
                dataOutputStream.writeBytes(array[j] + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
双色Hanoi问题是指有两堆不同颜色的盘子,从一侧开始按照规则放置,现在需要将这两堆盘子按照规则全部移到另一侧。这个问题可以使用递归算法来解决。 具体思路如下: 1. 将第一堆盘子的上面 n-1 个盘子移动到第三个柱子上(空柱子); 2. 将第一个柱子上的最后一个盘子移动到第二个柱子上; 3. 将第三个柱子上的 n-1 个盘子移动到第二个柱子上。 其中第一步和第三步可以看作是同一个问题,因此可以使用递归来解决。具体实现时,可以将这个问题看作是将 n 个盘子从第一个柱子移动到第二个柱子,其中第三个柱子作为辅助柱子。因此,递归函数可以定义为: ```python def move(n, source, dest, aux): if n == 1: print("Move disk", n, "from", source, "to", dest) else: move(n-1, source, aux, dest) print("Move disk", n, "from", source, "to", dest) move(n-1, aux, dest, source) ``` 在这个递归函数中,参数 n 表示要移动的盘子数,source、dest、aux 表示三个柱子的编号。当 n=1 时,直接将盘子从 source 移动到 dest 即可,如果 n>1,则需要先将上面的 n-1 个盘子移动到辅助柱子上,再将最后一个盘子从 source 移动到 dest,最后再将辅助柱子上的 n-1 个盘子移动到 dest 上。 使用这个递归函数可以解决双色Hanoi问题。不过需要注意的是,对于双色Hanoi问题,需要将两堆盘子分别看作两个独立的问题来处理。在递归的过程中,需要使用不同的辅助柱子来保证两个问题之间不会相互干扰。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值