Java 汉诺塔

问题描述

给定三根柱子A,B,C,柱子A上按照大小顺序放着n个大小不同的盘子,最下面的最大,最上面的最小,输入盘子的个数,现在要把柱子A上的盘子全部移动到C上。

问:
最少要移动多少次,输出每一步移动的方式。
输入开始计数的步骤和结束技术的步骤,统计在此期间每一个步骤出现的次数。

代码
package Ring1270.pra.java01;
import java.sql.SQLOutput;
import java.util.Scanner;
/**
 * https://www.lanqiao.cn/courses/2786/learning/?id=67029 */public class F_TowerOfHanoi {
    //Constants
 static int STEP = 0;
    static int AB = 0;
    static int AC = 0;
    static int BC = 0;
    static int BA = 0;
    static int CA = 0;
    static int CB = 0;
    //Method of count
 public static void count(String s) {
        if ("A-->B".equals(s)) AB++;
        if ("A-->C".equals(s)) AC++;
        if ("B-->A".equals(s)) BA++;
        if ("B-->C".equals(s)) BC++;
        if ("C-->A".equals(s)) CA++;
        if ("C-->B".equals(s)) CB++;
    }
    //Method of output
 public static void prin() {
        System.out.println("A-->B : " + AB);
        System.out.println("A-->C : " + AC);
        System.out.println("B-->A : " + BA);
        System.out.println("B-->C : " + BC);
        System.out.println("C-->A : " + CA);
        System.out.println("C-->B : " + CB);
    }
    //Method of tower's moving
 public static void move(int n, String from, String pass, String to, int x, int y) {
        if (n == 1) {
            System.out.println("Step" + ++STEP + ": " + "第1个盘子从" + from + "移动到" + to + ",记为:" + from + "-->" + to);
            if (STEP >= x) {
                String s = from + "-->" + to;
                count(s);
            }
        } else {
            //当有两个盘子的时候,这里相当于把第一个移动到B柱子上
 move(n - 1, from, to, pass, x, y);
            System.out.println("Step" + ++STEP + ": " + "将" + n + "个盘子从" + from + "移动到" + to + ",记为:" + from + "-->" + to);
            if (STEP >= x) {
                String s = from + "-->" + to;
                count(s);
            }
            //然后把第二个盘子移动,相当于把中间移动到C上面去
 move(n - 1, pass, from, to, x, y);
        }
    }
    //Method of main
 public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input the number of work's time:");
        int n = scanner.nextInt();
        System.out.println("Input the step of start:");
        int x = scanner.nextInt();
        System.out.println("Input the step of end:");
        int y = scanner.nextInt();
        if (x <= 0 || x > Math.pow(2, n) - 1 || x >= y) {
            throw new Exception("The start number is error , Runing is over......");
        }
        if (y <= 0 || y > Math.pow(2, n) - 1 || y <= x) {
            throw new Exception("The end number is error, Runing is over......");
        }
        int p = 0;
        move(n, "A", "B", "C", x, y);
        prin();
    }
}
运行截图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CBgSYSV2-1613976165100)(/img/bVcOMs9)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值