当前位置:网站首页>[C language] recursively explain the tower of Hanoi problem
[C language] recursively explain the tower of Hanoi problem
2022-07-27 04:25:00 【Anduin of the attack】
List of articles
Preface
Hanoi , It's a puzzle toy originated from an ancient Indian legend . Vatican made three diamond pillars when he created the world , Stack on a column from bottom to top in order of size 64 A golden disk . Brahman ordered Brahman to rearrange the disc from below on another pillar in order of size . 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 . When put 64 When the disc moves from the first stone pillar to the third stone pillar , The world will be destroyed .
And how long does it take for Brahmans to move the disc ? It is difficult to calculate by ordinary methods .
Today, we will use the idea of recursion to calculate the number of movements of the tower of Hanoi and the moving steps of the tower of Hanoi !
Hanoi Tower mobile diagram
The law of Hanoi Tower movement is that only one disc can be moved at a time , And the small market should be above the large market , Suppose that A Column has n Disc , Let's put n-1 A disk from A The column moves to B column , And then put the n A disk moves to C column , Finally, put n-1 A disk moves to C column .
Take the third-order Hanoi tower as an example :


The number of Hanoi Tower moves
Use exhaustive methods , Calculate the number of movements of Hanoi Tower :
| Order | frequency | law |
|---|---|---|
| 1 | 1 | 2^1-1 |
| 2 | 3 | 2^2-1 |
| 3 | 7 | 2^3-1 |
| 4 | 15 | 2^4-1 |
| … | … | 2^n-1 |
about n The number of times the tower of Hanoi moves :
- step 1 The number of steps involved is n-1 The number of times a disc needs to move , We can regard its steps as f(n-1).
- step 2 The number of steps involved is 1.
- step 3 The number of steps and steps involved 1 be similar , We also regard its steps as f(n-1).
Then observe the number of movements of Hanoi Tower in the table , For the first-order Hanoi Tower, the number of moves is 1, For other orders, it is the number of Hanoi Tower moves in the previous order + 1 + The number of moves of Hanoi Tower in the previous stage .
It is not difficult to get a recursive expression :f(n-1) + 1 + f(n-1) = 2 * f(n - 1) + 1
Moving steps conform to the idea of recursion , Code display :
#include<stdio.h>
int hanoi_step(int n)
{
if(n<=1)
return 1;
else
return 2*hanoi_step(n-1)+1;
}
int main()
{
int n = 0;
scanf("%d",&n);
int ret = hanoi_step(n);
printf("%d\n",ret);
return 0;
}
Running results :

Printing of Hanoi Tower
Printing here refers to the steps of printing the movement of Hanoi Tower .
We can try to write 1-4 The moving steps of step Hanoi Tower :
| Order | step |
|---|---|
| 1 | A->C |
| 2 | A->B,A->C,B->C |
| 3 | A->C,A->B,C->B,A->C,B->A,B->C,A->C |
| 4 | A->B,A->C,B->C,A->B,C->A,C->B,A->B,A->C,B->C,B->A,C->A,B->C,A->B,A->C,B->C |
We observe the moving steps , When only one disc is found, the moving steps are A->C; Two discs , by A->B,A->C,B->C.
So for n What about the tower of Hanoi , We deduce it :
- hold n-1 A disk from A Move to B
- The first n A disk from A Move to C
- hold n-1 A disk from B Move to C
that n-1 How to make a disc from A Move to B Well ?
- hold n-2 A disk from A Move to C
- The first n-1 A disk from A Move to B
- hold n-2 A disk from C Move to B
alike , For the n-1 A disk from B Move to C, It can also be inferred that :
- hold n-2 A disk from B Move to A
- The first n-1 A disk from B Move to C
- hold n-2 A disk from A Move to C
Through these deductions, we find , The movement of Hanoi tower can be expanded recursively , Then the above deduction steps , We can use it as a recursive step .
Ideas : Definition A,B,C Three characters , Express A,B,C Three columns , Definition n Is the order , that n-1 That is, in the move step , Number of discs to be moved .
For the first-order tower of Hanoi , Just move it , For other orders , You need to expand recursively , by n The moving steps of step Hanoi Tower .
#include<stdio.h>
void hanoi_move(int n,char A,char B,char C)
{
if(1==n)
{
printf("%c->%c\n",A,C);
}
else
{
hanoi_move(n-1,A,C,B);// take n-1 A disk from A Move to B
printf("%c->%c\n",A,C);// Will be the first n A disk from A The column moves to C column
hanoi_move(n-1,B,A,C);// take n-1 A disk from B The column moves to C column
}
}
int main()
{
int n = 0;
scanf("%d",&n);
hanoi_move(n,'A','B','C');
return 0;
}
It may not be intuitive just through code , We take the third-order Hanoi tower as an example , Observe how recursion unfolds :
Running results :

Conclusion
Understand the moving steps and process of Hanoi Tower , We can also 64 Make an estimate of the time it takes to move the Golden Disc , Think of each move as a second , Then the time is :2 ^ 64 - 1 = 18,446,744,073,709,551,615 second , Convert adult numbers , about 5800 In one hundred million, .
At this pace , At that time, it is also possible to destroy the world , It's just pathetic “ Tragic Brahman ” You need to move these discs (doge).
That's all C Language recursion solves all the contents of Hanoi Tower , If you feel anduin If it's well written , One click and three links, please !
I am a anduin, a C Language beginners , See you next time !
边栏推荐
- People don't talk much, engineers don't talk much
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- A. Round Down the Price
- Okaleido ecological core equity Oka, all in fusion mining mode
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- 2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
- Is VR panorama just needed now? After reading it, you will understand
- [competition reference] pytorch common code snippet and operation collection
- c# 获取uuid
- 【比赛参考】PyTorch常用代码段以及操作合集
猜你喜欢

C language learning notes - memory management

Remember the major performance problems caused by a TCP packet loss

shel自动设置目录权限

Deep analysis - dynamic memory management

Learning route from junior programmer to architect + complete version of supporting learning resources

js三种遍历数组的方法:map、forEach、filter

Detailed explanation of TCP protocol knowledge

Delete the whole line of Excel, and delete the pictures together

JS modify the key value of the object array

匿名命名管道, 共享内存的进程间通信理解与使用
随机推荐
Standard C language 11
spark练习案例(升级版)
VR panorama gold rush "careful machine" (Part 1)
js修改对象数组的key值
面试题 16.05. 阶乘尾数
Practice of microservice in solving Library Download business problems
每日一题:从链表中删去总和值为零的连续节点
面试题 02.05. 链表求和
JVM调优中的一些常见指令
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩
Webpack packaging Vue project adds confusion to solve the cache problem
记一次TCP丢包带来的重大性能问题
Elastic认证考试:30天必过速通学习指南
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
链表内指定区间反转
通信协议综述
Interview question 16.05 factorial mantissa
The new Internet era has come. What new opportunities will Web 3.0 bring us
新互联网时代已来 WEB 3.0 会给我们带来哪些新机遇