汉诺塔

汉诺塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市;1883年法国数学家Edouard Lucas曾提及这个故事,据说创世纪时Benares有一座波罗教塔,是由三支钻石棒(Pag)所支撑,开始时神在第一根棒上放置64
个由上至下依由小至大排列的金盘(Disc),并命令僧侣将所有的金盘从第一根石棒移至第三根石棒,且搬运过程中遵守大盘子在小盘子之下的原则,若每日仅搬一个盘子,则当盘子全数搬运完毕之时,此塔将毁损,而也就是世界末日来临之时。

解法

如果柱子标为ABC,要由A搬至C,在只有一个盘子时,就将它直接搬至C,当有两个盘子,就将B当作辅助柱。如果盘数超过2个,将第三个以下的盘子遮起来,就很简单了,每次处理两个盘子,也就是:A->B、A ->C、B->C这三个步骤,而被遮住的部份,其实就是进入程式的递回处理。

事实上,若有n个盘子,则移动完毕所需之次数为2^n - 1,所以当盘数为64时,则所需次数为:
2的64次方- 1 = 18446744073709551615为5.05390248594782e+16年,也就是约5000世纪,如果对这数字没什么概念,就假设每秒钟搬一个盘子好了,也要约5850亿年左右。

#include <stdio.h>
void hanoi(int n, char A, char B, char C) {if(n == 1) {//当只有一块时,A=>Cprintf("Move sheet %d from %c to %c\n", n, A, C);}else {hanoi(n-1, A, C, B);//将n-1块全部从A搬到B。printf("Move sheet %d from %c to %c\n", n, A, C);//剩下的一块搬到C。hanoi(n-1, B, A, C);//再将这n-1块从B,搬到C。}
}int main() {int n;printf("请输入盘数:");scanf("%d", &n);hanoi(n, 'A', 'B', 'C');return 0;
}

双色汉诺塔【分离型】
参考自博客

双色河内塔是由之前所介绍过的河内塔规则衍生而来,双色河内塔的目的是将下图左上的圆环位置经移动成为右下的圆环位置:具体见引用博客。清楚了题目后,再开始看代码。


#include <stdio.h>void hanoi(int disks, char source, char temp, char target) {if (disks == 1) {//注意,这里是两块不同颜色,相同大小的块,所以需要移动两次,才算一个大小的块printf("move disk from %c to %c\n", source, target);printf("move disk from %c to %c\n", source, target);} else {hanoi(disks-1, source, target, temp);hanoi(1, source, temp, target);hanoi(disks-1, temp, source, target);}
}void hanoi2colors(int disks) {char source = 'A';char temp = 'B';char target = 'C';int i;for(i = disks / 2; i > 1; i--) {hanoi(i-1, source, temp, target);//n-1块移到A=》Cprintf("move disk from %c to %c\n", source, temp);//A=>B两次printf("move disk from %c to %c\n", source, temp);hanoi(i-1, target, temp, source);//n-1块从C移回来,C=》Aprintf("move disk from %c to %c\n", temp, target);//最重要的一步来了,所有的步骤都是为了这一步,将最大的两块分离~}printf("move disk from %c to %c\n", source, temp);printf("move disk from %c to %c\n", source, target);
}int main() {int n;printf("请输入盘数:");scanf("%d", &n);hanoi2colors(n);return 0;
}

三色河内塔


#include <stdio.h>
void hanoi(int disks, char source, char temp, char target) {if (disks == 1) {printf("move disk from %c to %c\n", source, target);printf("move disk from %c to %c\n", source, target);printf("move disk from %c to %c\n", source, target);} else {hanoi(disks-1, source, target, temp);hanoi(1, source, temp, target);hanoi(disks-1, temp, source, target);}
}void hanoi3colors(int disks) {char source = 'A';char temp = 'B';char target = 'C';int i;if(disks == 3) {printf("move disk from %c to %c\n", source, temp);printf("move disk from %c to %c\n", source, temp);printf("move disk from %c to %c\n", source, target);printf("move disk from %c to %c\n", temp, target);printf("move disk from %c to %c\n", temp, source);printf("move disk from %c to %c\n", target, temp);;}else {hanoi(disks/3-1, source, temp, target);printf("move disk from %c to %c\n", source, temp);printf("move disk from %c to %c\n", source, temp);printf("move disk from %c to %c\n", source, temp);hanoi(disks/3-1, target, temp, source);printf("move disk from %c to %c\n", temp, target);printf("move disk from %c to %c\n", temp, target);printf("move disk from %c to %c\n", temp, target);hanoi(disks/3-1, source, target, temp);printf("move disk from %c to %c\n", target, source);printf("move disk from %c to %c\n", target, source);hanoi(disks/3-1, temp, source, target);printf("move disk from %c to %c\n", source, temp);for (i = disks / 3 - 1; i > 0; i--) {if (i>1) {hanoi(i-1, target, source, temp);}printf("move disk from %c to %c\n",target, source);printf("move disk from %c to %c\n",target, source);if (i>1) {hanoi(i-1, temp, source, target);}printf("move disk from %c to %c\n", source, temp);}}
}int main() {int n;printf("请输入盘数:");scanf("%d", &n);hanoi3colors(n);return 0;
}

汉诺塔(hanoi)、双色汉诺塔(分离型)、三色汉诺塔相关推荐

  1. Arduino使用三色\\双色LED

    剩下的37款传感器套件中还有4个跟LED相关的,本文就一并进行介绍. 1.三色LED 总共有两个三色LED,引脚类似,但是形状不一样,首先给出淘宝上的图片,左边的类似贴片LED,右边的头比较大: 然后 ...

  2. 海信 Vidda C1S 4K 三色激光投影仪 参数配置 海信C1S评测

    海信 Vidda C1S 4K 三色激光投影仪,搭载 LPU 三色激光引擎和 Hi-Fi 级定制 JBL 音响系统,包含了实测亮度提升至 1600ANSI 流明.色域提升至 110% BT.2020. ...

  3. 使用三色笔按照思维导图的方式记笔记

    前段时间,对思维导图进行了一些了解,学了一点皮毛.放到我现在的工作中来讲,最优先可以被使用的地方就是做会议记录之类的东西了.(开发人员有时候也需要记点东西的,其他公司不知道是不是也这样) 思维导图简介 ...

  4. “三色河内塔”算法(三色汉诺塔)

    问题引入 "三色河内塔"由"河内之塔"的规则衍生而来(点击查看),区别在于三色河内塔的目的是将图1所示的圆盘位置,移动成为图2所示的圆盘位置."三色河 ...

  5. 汉诺塔(Hanoi)问题归纳总结

    一.汉诺塔问题及其递归算法 1.问题阐述 经典汉诺塔: 外文算法书对汉诺塔问题的描述: 2.算法步骤 三阶汉诺塔问题解题步骤 共需7步. 四阶汉诺塔问题解题步骤 共需15步 五阶汉诺塔问题解题步骤 可 ...

  6. c语言程序设计电子图书 汉诺塔,用C写的汉诺塔(hanoi)程序

    用C写的汉诺塔(hanoi)程序 分类:计算机等级 | 更新时间:2016-07-07| 来源:转载 #include void movedisc(unsigned n,char fromneedle ...

  7. 【头歌】汉诺塔(Hanoi)的递归算法

    任务描述 本关任务:汉诺塔(Hanoi)的递归算法. 相关知识 相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到 ...

  8. PTA汉诺(Hanoi)塔问题

    PTA汉诺(Hanoi)塔问题 古代某寺庙中有一个梵塔,塔内有3个座A.B和C,座A上放着64个大小不等的盘,其中大盘在下,小盘在上.有一个和尚想把这64 个盘从座A搬到座B,但一次只能搬一个盘,搬动 ...

  9. 汉诺塔(Hanoi)递归算法

    相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘. 游戏的目标:把A杆上的金盘全部移到C杆 ...

最新文章

  1. java 模块化_Java 9 新特性 - 模块化 - Java 技术驿站-Java 技术驿站
  2. HBase 名称空间namespace的创建/建表/删除
  3. Nginx 配置内网访问树莓派4 ASP.NET Core 3.0 网站
  4. 工业交换机厂家有哪些,国产工业交换机品牌排行榜
  5. android 高德地图设置不能旋转_如何将平面控制点导入Google Earth、奥维互动地图及手机奥维互动地图APP里面?...
  6. vue图片裁剪:使用vue-cropper做图片裁剪
  7. 中国最酷、最美的女明星不是王菲,而是她!
  8. AMD64(x86_64)架构abi文档:上
  9. videoder有什么用_videoder
  10. dell笔记本外接显示器_笔记本就一个 hdmi 的接口,如何外接 2 个 dell 显示器?...
  11. VS Code中使用PlantUML绘图
  12. 毕业设计-基于机器学习的建筑能耗预测
  13. 浙大计算机跨专业考研心路历程记录
  14. C++知识点打结(一)
  15. 搜狗翻译加密原理分析
  16. 雅虎通——从怀念我的雅虎说起
  17. Arduino 下用A4988或TMC2209驱动42步进电机
  18. 我失业了?| ChatGPT生信分析初体验
  19. 界面设计常用的几种配色风格及文字底色
  20. 闲鱼转转源码+后台你懂的App

热门文章

  1. 7-4 输出两行短句 (10分)
  2. #今日论文推荐#CVPR 2022 | 道高一尺,魔高一丈,ConvNet还是ViT?
  3. 奋战7个月拿下offer【结果被裁,我哭了】
  4. ARMv7体系结构汇总
  5. 几个好用的扩展程序,谷歌和火狐的
  6. python交易是什么意思_py交易是什么意思 py交易是什么梗
  7. 如何才能降低亚马逊账号关联?
  8. Android接入支付宝实现支付功能
  9. 一图看懂Python生态圈图像格式转换
  10. IDEA 2020.1.2 无法显示图片的魔幻解决方法