当前位置:网站首页>[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)
边栏推荐
- Implementation of memcpy in C language
- What parameters should be passed in calling integer or character array functions
- [网络]跨区域网络的通信学习路由表的工作原理
- C language pointer and two-dimensional array
- Token verification program index.php when configuring wechat official account server
- Cloud computing notes part.1 - system management
- Labelme(一)
- How does app automated testing achieve H5 testing
- BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
- 11. Learn MySQL union operator
猜你喜欢

This customized keyboard turns me on~

数字图像理论知识(一)(个人浅析)
![[C language] header file of complex number four operations and complex number operations](/img/f9/b389fe5367f1fa6cd18aaac856bc0d.png)
[C language] header file of complex number four operations and complex number operations

leetcode day1 分数排名

Cdga | how can the industrial Internet industry do a good job in data governance?

Servlet learning notes

Codeignier framework implements restful API interface programming

并发程序设计,你真的懂吗?

How many types of rain do you know?

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
随机推荐
editor.md中markdown编辑器的实现
leetcode day3 超过经理收入的员工
[NPP installation plug-in]
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
How navicate modifies the database name
[C language] random number generation and `include < time. H > 'learning
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
Const pointer of C language and parameter passing of main function
NetCoreAPI操作Excel表格
Token verification program index.php when configuring wechat official account server
Function fitting based on MATLAB
How does app automated testing achieve H5 testing
数字图像理论知识(一)(个人浅析)
[网络]跨区域网络的通信学习路由表的工作原理
[wechat applet development] page navigation and parameter transmission
Overcome the "fear of looking at teeth", and we use technology to change the industry
Leetcode Day1 score ranking
Leetcode Day2 consecutive numbers