当前位置:网站首页>Tower of Hanoi classic recursion problem (C language implementation)
Tower of Hanoi classic recursion problem (C language implementation)
2022-07-29 04:31:00 【Small clock HHH】
Hanoi —— Classical recursion problem (c Language implementation )
The problem background
The tower of Hanoi problem is a classic one . Hanoi (Hanoi Tower), Also known as the river tower , From an old 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 , anytime , You can't zoom in on a small disk , And you can only move one disc at a time between the three pillars . Ask how to operate ?

Problem analysis
Ideas :
1. The language used :C Language
2. The compiler used :vs2019
3. Reference books : Tanhaoqiang Fourth Edition
4. Main knowledge used : Recursion of a function
5. The idea of code implementation is mainly divided into three steps :
Suppose a total of... Needs to be moved n null
1. take A On the column n-1 With the help of C The column moves to B column
2. take A The last plate left on the column moves to C column
3. take B On the column n-1 With the help of A The column moves to C column
Code implementation
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void move(int x, int y)
{
printf("%c->%c\n", x, y);
}
void hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
move(a, c);
}
else
{
hanoi(n - 1, a, c, b);// take A On the seat n-1 With the help of C The seat moves to B seat
move(a, c);// take A The last plate on the seat moves to C seat
hanoi(n - 1, b, a, c);// take B On the seat n-1 With the help of A The seat moves to C seat
}
}
//move Real participation in hanoi The formal parameters in the function correspond to , and hanoi Formal parameters in function a,b,c The corresponding value is also changing regularly
int main()
{
int n = 0;
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Running results
3
A->C
A->B
C->B
A->C
B->A
B->C
A->C
D:\c code\ Small question bank \ The hanotta problem \Debug\ The hanotta problem .exe ( process 13928) Exited , The code is 0.
Press any key to close this window . . .
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
D:\c code\ Small question bank \ The hanotta problem \Debug\ The hanotta problem .exe ( process 19616) Exited , The code is 0.
Press any key to close this window . . .
The code analysis
The illustration 
The core algorithm
if (n == 1)
{
move(a, c);
}
else
{
hanoi(n - 1, a, c, b);// take A On the seat n-1 With the help of C The seat moves to B seat
move(a, c);// take A The last plate on the seat moves to C seat
hanoi(n - 1, b, a, c);// take B On the seat n-1 With the help of A The seat moves to C seat
}
The judgment condition is n Is it 1, When n=1 End of time , Re pass move Function to print the required steps , The recursive process is really hard to describe , I can only understand it by myself
Personal research
1. You'll find that n The number of steps required for a plate is 2 Of n Power of minus one , And the middle step is always A–>C
2. Why is the first step always A–>C, And even hours , The first step is always A–>B?
explain :hanoi(n - 1, a, c, b) Arguments to functions a,b and c Not always corresponding A disc ,B Plate and C Disk and after passing it to the formal parameter abc The corresponding value will change , But it is changing regularly , Why do you want to know this ? Because of this hanoi The value corresponding to the formal parameter of the function will affect move Function printing . Rules are formal parameters abc The corresponding value is ACB,ABC Cycle change , Plus the first call hanoi Function parameter abc Corresponding ABC, This explains why the first step is always A–>C, And even hours , The first step is always A–>B
experience
So hard , It took me a whole afternoon to understand , I also read the explanation in the book , Read other people's explanations , The above analysis diagram is copied (hhh), Just a few examples , Implement the code again and again, and then understand , Find out the rules . Recursive problems are abstract , It's hard to imagine the running result , You can do some simple recursive problems to understand , Take your time , Have a headache , Ha ha ha
If there is a mistake , Also please correct me , thank you !
边栏推荐
猜你喜欢

Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan

Jenkins 参数化构建中 各参数介绍与示例

不会就坚持67天吧 平方根

Deploy Jenkins using containers

不会就坚持66天吧 权重生成随机数

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

redux快速上手

post导出数据,返回

Common components of solder pad (2021.4.6)
![[express connection to MySQL database]](/img/a6/d68327fa74b8c94d250ea469301839.png)
[express connection to MySQL database]
随机推荐
What is the difference between field, variable and property
[c language] PTA 7-48 find the number of combinations
C language: summary of consortium knowledge points
Webrtc realizes simple audio and video call function
Shell string segmentation
Labelme cannot open the picture
Not for 63 days. The biggest XOR
Deep learning training strategy -- warming up the learning rate
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
Jenkins 参数化构建中 各参数介绍与示例
Pytorch GPU and CPU models load each other
11.备份交换机
C语言:联合体知识点总结
Can you really write restful API?
Not 67 days, square root
15.federation
kotlin的List,Map,Set等集合类不指定类型
Don't stick to it for 68 days. Baboons eat bananas
不会就坚持71天吧 链表排序
Won't you just stick to 69 days? Merge range