当前位置:网站首页>[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)
边栏推荐
- The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
- A chip company fell in round B
- 爬取IP
- 毕马威中国:证券基金经营机构信息技术审计项目发现洞察
- Implementation of strcat in C language
- Prometheus deployment
- Longest Palindromic Substring
- This customized keyboard turns me on~
- There are five certificates in the soft examination advanced examination, which is more worth taking?
- [网络]跨区域网络的通信学习路由表的工作原理
猜你喜欢

How to write the SQL statement of time to date?

Digital filter design matlab

Cloud computing notes part.2 - Application Management

English Translation Spanish - batch English Translation Spanish tools free of charge

Information management system and games based on C language

Advanced notes (Part 2)

Implementation of markdown editor in editor.md

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

Common APIs in string

How does app automated testing achieve H5 testing
随机推荐
[C language] summary of methods for solving the greatest common divisor
克服“看牙恐惧”,我们用技术改变行业
Implementation of markdown editor in editor.md
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
Array method added in ES6
Circular linked list OJ question
你知道雨的类型有几种?
Leetcode day3 employees who exceed the manager's income
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
[wechat applet development] page navigation and parameter transmission
基于C语言的信息管理系统和小游戏
Source insight project import and use tutorial
Const pointer of C language and parameter passing of main function
editor.md中markdown编辑器的实现
MySQL8 Encrypting InnoDB Tablespaces
Source code analysis of scripy spider
数字图像理论知识(一)(个人浅析)
leetcode day5 删除重复的电子邮箱
Leetcode Day5 delete duplicate email
CodeIgnier框架实现restful API接口编程