当前位置:网站首页>Programming learning record -- recursively solving the tower of Hanoi problem
Programming learning record -- recursively solving the tower of Hanoi problem
2022-07-27 06:23:00 【Autumn mountain chariot God AE】
1. The understanding of recursion , Pass and return , First, we need to design a benchmark situation , This benchmark case , In some cases , It can solve without recursion .
2. Then the problems encountered can be decomposed into benchmark cases , And non benchmark cases , Non benchmark cases can also be decomposed into benchmark cases and non benchmark cases .
3. Such as the tower of Hanoi , Suppose the number of discs is 4, Then the top three discs can be regarded as a whole , Form four discs with the lowest disc , The benchmark case is how to move when there is only one disk .4 The order is decomposed into 3 Step sum 1 rank ,3 The order is decomposed into 2 Step sum 1 rank ,2 Decompose into 1 Step sum 1 rank .
4. in fact , In the case of the tower of Hanoi ,1 The movement of the order can be seen as from A The column moves directly to C column
5. And in the second order , First step A First move to B, The second step A Then move to C, The third step B Move to C.
6. We know , When the first order A Move directly to C, therefore , We can regard the first three steps as non benchmark , The second step is to look at the benchmark situation .
7. First step , Can be seen as A It's the starting post ,C For the middle post ,B Is the first-order case of the target column , Third parts , It can be seen as B Is the starting column ,A It's the middle column ,C Is the first-order case of the target column .
8. So the second-order movement can be divided into three steps ,
1. saucer A<C>B First order movement of .2. Large disc A Move to C.3. saucer B<A>C First order movement of .
In the third order, the top two discs are regarded as a whole , The bottom disc is the reference case , The fourth order regards the top three as a whole ........
void move(char A, char C)
{
printf("%c Column top disc moves to %c column \n", A, C);
}
void HNT(char A, char B, char C,int n)
{
if (n == 1) move(A, C);
else
{
HNT(A, C, B, n - 1);
move(A, C);
HNT(B, A, C, n - 1);
}
}
int main()
{
char A = 'a';char B = 'b';char C = 'c';//A Starting column ,B Transfer column ,C Target column
int x = 4;// Number of discs
HNT(A, B, C, x);
return 0;
}边栏推荐
- Index and transaction of database (emphasis)
- 遥感影像识别-制作数据集
- 5g's past and present life -- a brief introduction to the development of mobile communication
- What is the difference between single line and three line when renting servers in Hong Kong?
- Sexy prime number (acwing daily question)
- Wireshark graphical interface capture
- C language minesweeping latest recursive expansion super detailed explanation (with source code)
- UnityShader-LowPoly
- Reading and writing of file content - data flow
- Common SQL optimization methods
猜你喜欢

Learning the operation environment needs to be equipped during software testing

Li Kou 236. the nearest common ancestor of binary tree

数据库的约束以及设计

Interpretation of unity desktop version 7.6

Unable to start program, access denied?

Unity engine starts to migrate from mono to.Net coreclr

Wireshark packet modification -- adding or modifying message fields (2)

ROS节点名称重名

Remote sensing image recognition imaging synthesis
Calculation of Huffman tree, code implementation and proof, graphic interpretation
随机推荐
Navigation related messages
Strategies for common locks in multithreading
如何选择正确的服务器备份方法
Multi coordinate transformation
数据库在终端的基础操作
Code implementation and introduction of all commonly used sorting
[dynamic planning - steel bar cutting]
Related knowledge of internal classes
Understand the pointer in a picture
Remote sensing image recognition training strategy
Related knowledge of multithreading
How to distinguish an independent server from a VPS host?
ROS中的头文件与源文件
wireshark数据包修改--IP地址修改(一)
Programming learning records - Lesson 3 [first knowledge of C language]
Wireshark function introduction
ROS distributed communication
Socket long link
Index and transaction of database (emphasis)
Comparison of communication mechanisms