当前位置:网站首页>How to play the Tower of Hanoi
How to play the Tower of Hanoi
2022-08-04 13:06:00 【ふり】
作者 :ふり
专栏 :JavaSE
格言 : I came ; I saw ; I conquer
汉诺塔
- 汉诺塔问题源自印度一个古老的传说,印度教的“创造之神”梵天创造世界时做了 3 根金刚石柱,其中的一根柱子上按照从小到大的顺序摞着 64 个黄金圆盘.梵天命令一个叫婆罗门的门徒将所有的圆盘移动到另一个柱子上,移动过程中必须遵守以下规则:
每次只能移动柱子最顶端的一个圆盘;- 每个柱子上,
小圆盘永远要位于大圆盘之上;
思路讲解
When the starting column is only on 1 个圆盘时,We can easily move it to the target bar

A ->C
When there is on the starting column 2 个圆盘时,先将AMove to the small plate on topB上;再将AThe large plate on the move toC上,最后将BMove to the small plate on topC上即可

A -> B 、 A -> C、 B -> C
如果是三个盘子,先将A上的盘子移动到C上;再将A上的盘子移动到B上,再将C上的盘子移动到B,再将A 上的盘子移动到C,will go upB的盘子移动到A,再将B的盘子移动到C,最后将A移动到C即可.
A -> C 、 A -> B 、 C -> B 、 A -> C 、 B -> A 、 B - > C 、 A -> C
According to three examples can be found,Except when there is only one plate.The plate is moving toC的过程中会有 n-1 个盘子在B上暂存.
两个盘子 n-1 There will be a plateB上暂存
三个盘子 n-1 There will be two platesB上暂存
3One plate of the Tower of Hanoi problem
思路
- 借助C把 n-1 个盘子移动到B
- 把AMove the remaining plates toC
- 借助A把 n-1 个盘子移动到C
public class hannuota {
/** * @name 递归求解汉诺塔 * @param str 起始位置 * @param transit 中转位置 * @param end 目标位置 * **/
public static void hanio(char str, char transit, char end, int number) {
if (1 == number) {
//只有一个盘子
//Move the tray paper directly to C
move(str, end);
return;
}else {
//The plate is larger than1个
//此时 transit 是目标位置;而 end 是中转位置
hanio(str, end, transit, number - 1);//借助C将n-1个盘子移动到B上
move(str, end);
//此时 start 是中转位置,而end是目标位置
hanio(transit, str, end, number - 1);//借助A把n-1个盘子移动到C上
}
}
/** * @param str 起始位置 * @param transit 目标位置 **/
public static void move(char str, char transit) {
System.out.print(str+"->"+ transit + " ");
}
public static void main(String[] args) {
hanio('A', 'B', 'C', 1);
System.out.println();
hanio('A', 'B', 'C', 2);
System.out.println();
hanio('A', 'B', 'C', 3);
System.out.println();
hanio('A', 'B', 'C', 4);
}
}

边栏推荐
- router---Programmatic navigation
- Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
- 到底什么是真正的HTAP?
- oracle sql中根据条件判断是否插入数据
- 【水一个徽章】
- 【解决方案 三十一】Navicat数据库结构同步
- MATLAB——图像分块
- ES 节点2G内存分析
- A Collection of Flutter Tutorials (2022 Edition)
- router---mode
猜你喜欢

Week 7 Latent Variable Models and Expectation Maximization

小程序对接企业微信客服
![Valentine's Day Romantic 3D Photo Wall [with source code]](/img/a9/2c26f4f048f3c0a9a65551bc734233.png)
Valentine's Day Romantic 3D Photo Wall [with source code]

持续交付(二)PipeLine基本使用

CReFF缓解长尾数据联邦学习(IJCAI 2022)

【解决方案 三十一】Navicat数据库结构同步

代码越写越乱?那是因为你没用责任链!

SCA兼容性分析工具(ORACLE/MySQL/DB2--->MogDB/openGauss/PostgreSQL)

使用SQLServer复制数据库

Diffusion Models:生成扩散模型
随机推荐
The head module of the yolo series
oracle sql中根据条件判断是否插入数据
ES 节点2G内存分析
JSX use
到底什么是真正的HTAP?
《会面》-凯瑟琳曼斯菲尔德(徐志摩译)
COMSOL空气反应 模型框架
Interviewer: How to view files containing abc string in /etc directory?
MFC的相机双目标定界面设计
【PHP实现微信公众平台开发—基础篇】第2章 微信公众账号及申请流程详解
Access Huawei game anti-addiction, click the anti-addiction pop-up window, the game crashes
项目里的各种配置,你都了解吗?
座舱人机交互「暗潮汹涌」,语音「下」,多模态「上」
router---编程式导航
redisTemplate存取List遇到的坑
绩效考核带给员工的不能只是压力
博尔赫斯-诗中的经典语段
A discussion of integrated circuits
This article sorts out the development of the main models of NLP
荧光磷脂PEG衍生物之一磷脂-聚乙二醇-荧光素,Fluorescein-PEG-DSPE
