java优秀算法河内之塔_河内塔的Java程序

java优秀算法河内之塔

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are:

河内塔是一个数学难题,其中我们有三个杆和n个盘。 难题的目的是使用第三个杆(例如辅助杆)将所有磁盘从源杆移动到目标杆。 规则是:

  1. Only one disk can be moved at a time.

    一次只能移动一个磁盘。

  2. A disk can be moved only if it is on the top of a rod.

    仅当磁盘位于杆的顶部时才能移动磁盘。

  3. No disk can be placed on the top of a smaller disk.

    不能将磁盘放置在较小磁盘的顶部。

Print the steps required to move n disks from source rod to destination rod. Source Rod is named as 'a', auxiliary rod as 'b' and destination rod as 'c'.

打印将n个磁盘从源棒移到目标棒所需的步骤。 源杆称为'a' ,辅助杆称为'b' ,目的杆称为'c'

Input format: Integer n

输入格式:整数n

Output format: Steps in different lines (in one line print source and destination rod name separated by space).

输出格式:不同行中的步骤(在一行中,打印源和目标杆名称用空格分隔)。

Example:

例:

    Sample Input:
2
Sample Output:
a b
a c
b c

Explanation:

说明:

This is one of the famous problems on recursion. To solve this, let's assume the steps taken for 2 disks. Let’s assume Rod A, B, and C and we have to shift the disks from A to B. First, we shift the smaller disk to C, then we shift the larger disk to B, and at last, put the smaller disk from C to B.

这是递归的著名问题之一。 为了解决这个问题,我们假设对2个磁盘采取了步骤。 假设杆A,B和C,我们必须将磁盘从A移到B。首先,将较小的磁盘移至C,然后将较大的磁盘移至B,最后,将较小的磁盘从C移入到B。

Therefore, for N disks, lets recursion shifts N-1 disks to C, and we will shift the last disk to B and again let recursion shifts rest of the disk to C. Using this, we will be able to solve the problem.

因此,对于N个磁盘,让递归将N-1个磁盘移至C,然后将最后一个磁盘移至B,再让递归将其余磁盘移至C。使用此方法,我们可以解决问题。

Algorithm:

算法:

Declare a recursive function towerOfHanoi with parameters (int disk, char source, char auxiliary, char destination)

声明带有参数(int磁盘,char源,char辅助,char目标)的递归函数towerOfHanoi

  • STEP 1: Base Case: If(disk == 0) return;

    步骤1:基本情况: If(disk == 0)返回;

  • STEP 2: Base Case: If(disk == 1) Print Source to Destination

    步骤2:基本情况: If(disk == 1)打印源到目标

  • STEP 3: Recursive Case: towerOfHanoi(disk -1, source, destination, auxiliary)

    步骤3:递归案例: towerOfHanoi(磁盘-1,源,目标,辅助)

  • STEP 4: Print Source to Destination

    步骤4:将源打印到目标

  • STEP 5: towerOfHanoi(disk -1, auxiliary, source,destination)

    步骤5: TowerOfHanoi(磁盘-1,辅助,源,目标)

Example:

例:

    Input : 3
Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C

Program:

程序:

import java.util.Scanner;
public class Main {
//Recursive Function
public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) { 
// Write your code here
if(disks==0){   //Base Case 1
return;
}
if(disks==1){   //Base Case 2
System.out.println(source +" " + destination);
return;
}
else{
//Shifting d-1 disk from A to C
towerOfHanoi(disks-1,source,destination,auxiliary);   
System.out.println(source + " " + destination);
//Shifting d-1 disk from c to B
towerOfHanoi(disks-1,auxiliary,source,destination);   
}
}
public static void main(String[] args) {
int disk;
Scanner s = new Scanner(System.in);
System.out.print("Print No of Disks: ");
disk = s.nextInt();
towerOfHanoi(disk, 'A', 'C', 'B');
}
}

Output

输出量

Print No of Disks: 3
A B
A C
B C
A B
C A
C B
A B

翻译自: https://www.includehelp.com/java-programs/tower-of-hanoi.aspx

java优秀算法河内之塔

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377603.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

转——C# DataGridView控件 动态添加新行

DataGridView控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态…

分享通用基类库-C#通用缓存类

1 /************************************************************************************* 2 * 代码:吴蒋 3 * 时间:2012.03.30 4 * 说明:缓存公共基类 5 * 其他: 6 * 修改人: 7 * 修改时间: 8 * 修改说明: 9 ******************…

二、PyTorch加载数据

一、常用的两个函数 dir()函数可以理解为打开某个包,help()可以理解为返回如何使用某个具体的方法 例如:若一个A钱包里面有a,b,c,d四个小包,则可通过dir(A),打开该A钱包,返回a&…

leetcode 1005. K 次取反后最大化的数组和 思考分析

题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。) 以这种方式修改数组后,返回数组可能…

三、TensorBoard

一、安装TensorBoard 管理员身份运行Anaconda Prompt,进入自己的环境环境 conda activate y_pytorch,pip install tensorboard 进行下载,也可以通过conda install tensorboard进行下载。其实通俗点,pip相当于菜市场,c…

IT资产管理系统SQL版

你难道还在用Excel登记IT资产信息吗? 那你一定要好好考虑如何面对以下问题 1:IT人员需要面对自身部门以下问题用户申请了资产it部未处理的单还有哪些?库存里面还有哪些资产?有多少设备在维修?有多少设备已经报废了?哪些资产低于安全库存需要采购?使…

详细讲解设计跳表的三个步骤(查找、插入、删除)

目录写在前面跳表概要查找步骤插入步骤删除步骤完整代码写在前面 关于跳表的一些知识可以参考这篇文章,最好是先看完这篇文章再看详细的思路->代码的复现步骤: Redis内部数据结构详解(6)——skiplist 关于跳表的插入、删除基本操作其实也就是链表的插入和删除,所…

php 类静态变量 和 常量消耗内存及时间对比

在对类执行100w次循环后, 常量最快,变量其次,静态变量消耗时间最高 其中: 常量消耗:101.1739毫秒 变量消耗:2039.7689毫秒 静态变量消耗:4084.8911毫秒 测试代码: class Timer_profi…

一个机器周期 计算机_计算机科学组织| 机器周期

一个机器周期 计算机机器周期 (Machine Cycle) The cycle during which a machine language instruction is executed by the processor of the computer system is known as the machine cycle. If a program contains 10 machine language instruction, 10 separate machine …

四、Transforms

transform是torchvision下的一个.py文件,这个python文件中定义了很多的类和方法,主要实现对图片进行一些变换操作 一、Transforms讲解 from torchvision import transforms#按着Ctrl,点击transforms进入到__init__.py文件中 from .transfo…

leetcode 134. 加油站 思考分析

目录题目1、暴力法,双层遍历2、贪心题目 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发&#xff0…

单链线性表的实现

//函数结果状态代码#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //Status是函数的类型,其值是函数结果状态代码 typedef int Status; typedef int ElemType;…

时间模块,带Python示例

Python时间模块 (Python time Module) The time module is a built-in module in Python and it has various functions that require to perform more operations on time. This is one of the best modules in Python that used to solve various real-life time-related pro…

五、torchvision

一、下载CIFAR-10数据集 CIFAR-10数据集官网 通过阅读官网给的解释可以大概了解到,一共6w张图片,每张图片大小为3232,5w张训练图像,1w张测试图像,一共由十大类图像。 CIFAR10官网使用文档 torchvision.datasets.CIF…

leetcode 69. x 的平方根 思考分析

题目 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回…

背包问题 小灰_小背包问题

背包问题 小灰Prerequisites: Algorithm for fractional knapsack problem 先决条件: 分数背包问题算法 Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fraction…

360浏览器兼容问题

360浏览器兼容问题 360浏览器又是一大奇葩,市场份额大,让我们不得不也对他做些兼容性处理。 360浏览器提供了两种浏览模式,极速模式和兼容模式,极速模式下是webkit内核的处理模式,兼容模式下是与IE内核相同的处理模式。…

转 设计师也需要了解的一些前端知识

一、常见视觉效果是如何实现的 一些事 关于文字效果 互联网的一些事 文字自身属性相关的效果css中都是有相对应的样式的,如字号、行高、加粗、倾斜、下划线等,但是一些特殊的效果,主要表现为ps中图层样式中的效果,css是无能为力的…

六、DataLoader

一、DataLoader参数解析 DataLoader官网使用手册 参数描述dataset说明数据集所在的位置、数据总数等batch_size每次取多少张图片shuffleTrue乱序、False顺序(默认)samplerbatch_samplernum_workers多进程,默认为0采用主进程加载数据collate_fnpin_memorydrop_las…

单调栈 leetcode整理(一)

目录单调栈知识402. 移掉K位数字1673. 找出最具竞争力的子序列316. 去除重复字母(1081. 不同字符的最小子序列)321. 拼接最大数单调栈知识 单调栈就是一个内部元素有序的栈(大->小 or 小->大),但是只用到它的一…