当前位置:网站首页>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);
}
}
边栏推荐
- Django框架MySQL数据库到models模型的映射关系
- A Collection of Flutter Tutorials (2022 Edition)
- Why is Luo Zhenyu's A-share dream so difficult to fulfill?
- 技术分享| 小程序实现音视频通话
- 秋招攻略秘籍,吃透25个技术栈Offer拿到手软
- router---mode
- MySQL性能指标TPS\QPS\IOPS如何压测?
- COMSOL空气反应 模型框架
- Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
- 正确使用Impala的invalidate metadata与refresh语句
猜你喜欢
RobotFramework二次开发(一)
leetcode 48. Rotate Image 旋转图像(Medium)
做项目管理有且有必要了解并学习的重要知识--PMP项目管理
How to stress the MySQL performance indicators TPS\QPS\IOPS?
Chinese valentine's day of young people crazy to make money, earn 140000 a week
【PHP实现微信公众平台开发—基础篇】第1章 课程介绍
Interviewer: How to view files containing abc string in /etc directory?
This article sorts out the development of the main models of NLP
一文梳理NLP主要模型发展脉络
Arduino框架下I2S控制ADC采样以及PWM输出示例解析
随机推荐
Cows 树状数组
Niuke.com Brush Question Record || Linked List
Geoffrey Hinton:深度学习的下一个大事件
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
“蔚来杯“2022牛客暑期多校训练营4 N
【Game Of AutoTest】1、再度启程,重识游戏自动化测试
Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
5 cloud security management strategies enterprises should implement
Control CD-ROM with VbScript
Matlab记录
智能电视可以打开小程序应用,再也不用头痛内存了
router---动态路由匹配
干货丨数学规划视角下的分货优化解题思路
HDU1580 输出先手能取的方案数
Motion Rule (16)-Union Check Basic Questions-Relations
从零开始配置 vim(7)——自动命令
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
JSX使用
牛客网刷题记录 || 链表
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......