当前位置:网站首页>Recursive implementation of the Tower of Hanoi problem
Recursive implementation of the Tower of Hanoi problem
2022-07-31 04:01:00 【GD_small_bit】
I brought the Tower of Hanoi todayC语言实现代码.
汉诺塔(Tower of Hanoi),又称河内塔,是一个源于印度古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上.并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘.
There are three pillars as follows:
游戏规则如下:在a柱上会有n个盘子,And the big plate is below,The small plate is on top,每次只能移动一个盘子,Press the plate from largest to smallestcThe pillars are arranged upwards.
Suppose there is only one plate,Then we take the plate directly fromacolumn moved toc柱,即a->c.
假设aThere are two plates on the column,Then we should move the plate to the smaller oneb柱,Then move the larger platec盘,再将bThe plate on the column is moved toc柱,即 a->b , a->c , b->c This also completes the mission of the game.
假设a柱上有三个盘子,Then we should remove the smallest plate froma柱移到c柱,Then remove the smaller plate froma柱移到b柱,Then remove the smallest plate fromc柱移到b柱,再将最大的盘子从a柱移到c柱,再将bThe column with the smallest column moves toa柱,再将bMove the column to the smaller platec柱,最后把aMove the column plate toc柱.即 a->c , a->b , c->b , a->c , b->a , b->c , a->c .
下面是代码实现.
#include<stdio.h>
void move (char pos1,char pos2)
{
printf(" %c->%c ",pos1,pos2);
}
void Hanoi (int n ,char pos1,char pos2,char pos3)
{
if(n==1)
{
move(pos1,pos3);
}
else
{
Hanoi(n-1,pos1,pos3,pos2);
move(pos1,pos3);
Hanoi(n-1,pos2,pos1,pos3);
}
}
int main ()
{
Hanoi(1,'a','b','c');
printf("\n");
Hanoi(2,'a','b','c');
printf("\n");
Hanoi(3,'a','b','c');
printf("\n");
}
运行结果如下:
如果觉得写的可以,关注点一点,下期更精彩.
边栏推荐
- 数据库文件中的未分配的块和未使用的块的区别
- LocalDate加减操作及比较大小
- 《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
- 强化学习:从入门到入坑再到拉屎
- SIP Protocol Standard and Implementation Mechanism
- PMP WeChat group daily exercises
- Mysql 45 study notes (twenty-four) MYSQL master-slave consistency
- 已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
- Redis 使用 sorted set 做最新评论缓存
- The use of beforeDestroy and destroyed
猜你喜欢

TCP和UDP详解

Postgresql 15 source code analysis (5) - pg_control

IDEA常用快捷键与插件

Ambiguous method call.both

binom二项分布,
![[Swift]自定义点击APP图标弹出的快捷方式](/img/d4/84b237995fc3d3700916eb57f6670d.png)
[Swift]自定义点击APP图标弹出的快捷方式

立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业

Mysql 45 study notes (23) How does MYSQL ensure that data is not lost

【论文阅读】Mastering the game of Go with deep neural networks and tree search

No qualifying bean of type 问题
随机推荐
The BP neural network
已解决:不小心卸载pip后(手动安装pip的两种方式)
LeetCode每日一练 —— 138. 复制带随机指针的链表
IDEA common shortcut keys and plug-ins
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
端口排查步骤-7680端口分析-Dosvc服务
LocalDate加减操作及比较大小
Automation strategies for legacy systems
volatile内存语义以及实现 -volatile写和读对普通变量的影响
[C language] Preprocessing operation
els 方块向右移
LeetCode每日一练 —— OR36 链表的回文结构
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
Smartcom Programming Level 4 - Magic Academy Lesson 6
[shell basics] determine whether the directory is empty
el-image标签绑定点击事件后没有有用
Safety 20220712
IDEA comment report red solution
Mysql 45 study notes (23) How does MYSQL ensure that data is not lost
Component pass value provide/inject


