当前位置:网站首页>[C language] Hanoi Tower problem [recursion]
[C language] Hanoi Tower problem [recursion]
2022-07-28 20:04:00 【An ran_】
One 、 The problem background
Hanoi (Tower of Hanoi), also called Hanoi Tower , It's one that comes from India Ancient legend Souptoys . brahma When he created the world, he made three diamond pillars , Stack on a column from bottom to top in order of size 64 A golden disk . Brahma commanded Brahmin Rearrange the disc on another column in order of size from below . And stipulate , You can't enlarge a disc on a small disc , Only one disc can be moved between the three pillars at a time .
Two 、 Thought analysis
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-8KDqj3zJ-1651423284906)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220502003714910.png)]](/img/8a/d8656ddab71bcabbc899cdece6d1ab.png)
3、 ... and 、 Code demonstration
Particular attention : there ’A ‘B’'C ' It is different from the meaning of thinking analysis in the picture
// Hanoi
#include <stdio.h>
void move(char pos1, char pos2)
{
char pos3;
pos3 = pos1;
pos1 = pos2;
pos2 = pos3;
}
void Hanio(int n, char pos1, char pos2, char pos3)
{
if (n == 1)
move(pos1, pos3);
else
{
Hanio(n - 1, pos1, pos3, pos2);
printf("%c->%c\n", pos1, pos3);
move(pos1, pos3);
Hanio(n - 1, pos2, pos1, pos3);
}
}
int main()
{
Hanio(3, 'A', 'B', 'C');
return 0;
}
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-cmdjKLn0-1651423284908)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220502003826276.png)]](/img/d8/ff66928c2bc2ad906e38a360a8cf94.png)
边栏推荐
- 通信网络基础知识01
- C language implementation of strncpy
- Implementation of memcpy in C language
- [experience] some suggestions and experience on repairing electronic equipment
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- 【NPP安装插件】
- How navicate modifies the database name
- Use of strtok and strError
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Overcome the "fear of looking at teeth", and we use technology to change the industry
猜你喜欢
![[C language] initial C language reflection and summary](/img/21/826d144867f7a73ec2cd8896a5250a.png)
[C language] initial C language reflection and summary

How navicate modifies the database name

Handan, Hebei: expand grassroots employment space and help college graduates obtain employment

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)

English translation Arabic - batch English translation Arabic tools free of charge

云计算笔记part.1——系统管理

你知道雨的类型有几种?

Rand function generates pseudo-random numbers

How does app automated testing achieve H5 testing

Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
随机推荐
利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
个人博克系统登录点击图形验证码的集成与实现
【经验之谈】关于维修电子设备的几点建议和经验
云计算笔记part.1——系统管理
Labelme (I)
XOR operation and its usage
Edge detection and connection of image segmentation realized by MATLAB
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
How does app automated testing achieve H5 testing
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Deploy ZABBIX automatically with saltstack
This customized keyboard turns me on~
Overcome the "fear of looking at teeth", and we use technology to change the industry
Token verification program index.php when configuring wechat official account server
数字图像理论知识(一)(个人浅析)
leetcode day3 超过经理收入的员工
Leetcode day2 连续出现的数字
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
数字滤波器设计——Matlab