From recursion to the Tower of Hanoi

Recursion, as everyone knows, the famous Fibonacci number is a classic example of this knowledge point.
Today, let’s take a look at the more classic recursive problem Tower of Hanoi and Frog Jumping Steps , but this is actually a mathematical problem. Let's take a look at the Tower of Hanoi first.

Tower of Hanoi

The French mathematician Edward Lucas once wrote an ancient Indian legend: In the holy temple of Benares (in northern India) in the center of the world, three gem needles are inserted on a brass plate. When the main god of Hinduism, Brahma, created the world, 64 gold pieces from large to small were worn on one of the needles from bottom to top. This is the so-called Tower of Hanoi. No matter day or night, there is always a monk moving these gold pieces according to the following rule: only one piece at a time, no matter which needle is on, the small piece must be on top of the large piece. The monks predicted that when all the gold pieces were moved from the needle worn by Brahma to another needle, the world would be wiped out with a thunderbolt, and the pagoda, temples, and sentient beings would all perish.

This legend is quite interesting. This legend says that there are 64 gold pieces. But we are going to discuss whether there are only 3 or 4 sequins. Take a look at the net map.
Insert picture description here
Insert picture description here
We start the discussion with three pieces. At the beginning of the discussion topic, understand that the position of this column does not affect the movement, but the specific column is the key.
Let's see how many times to move if there is only one sequin.

Insert picture description here
Obviously, just one time is enough.
Let's take a look at two gold pieces.
Insert picture description here

Insert picture description here
Since we have to move all to the C pillar

1:
a:A->B
2:
b:A->C
3;
a:B->C

You can move all over in three steps.
Or if you want to move slice b, you need to move slice a first, leaving the empty C-pillar for slice b to consider the movement of slice a.
Let's take a look at the movement of the three pieces.
Insert picture description here
Then according to the above idea, if you want to move the c piece, you have to move the a and b pieces first, leaving the C pillar to the c piece.
This condition must be met first,
Insert picture description here
and the big cannot be above the small. It must be moved like this first.
Insert picture description here
And, if you want to be satisfied with this appearance, first.
Insert picture description here
in conclusion,

first step

Insert picture description here

Second step

Insert picture description here

third step

Insert picture description here
Looking at it this way, it is no different from the previous discussion that only moved two pieces.
Then move the c slice to the C column.
Insert picture description here
Became like this. Let's take a look again. As I said at the beginning of the article, the position of the column is not critical. The position can be changed, but it must be moved to a specific column. Now it looks like, did we go to move the two sequins earlier?
Insert picture description here
See clearly, this looks like the steps of moving two sequins. Then follow the steps above and it will do.
Let's calculate the writing steps

The first part:
move two sequins to the B-pillar.
The second part:
move the C piece to the C pillar. The
third part:
move the two sequins to the C pillar.

To sum it
up, the steps of moving two sequins, move twice, and then move the bottom sequin again. A total of 2*3+1==7 times.

Let's look at the steps of the four sequins.

If you want to move these four sequins, you must move the bottom sequin

Insert picture description here
Looking at it this way, there are two parts.

One:
Move the d piece to the C column.
Two:
Move the three gold pieces abc to the C column.

Analogy with moving three pieces above,

First purpose

Insert picture description here

Second purpose

Insert picture description here

Third purpose

Insert picture description here
It can be done in three parts. It can be seen that the three-piece movement method needs to be moved twice.
That is 2*7+1==15 times.
**

So on and so forth

**
When moving N pieces of sequins, first consider,
one -> move the N-1 pieces of sequins.
Two -> move the Nth slice again,
three -> move the N-1 slice to the destination again.
It can be divided into three steps.
The position of the pillar has no effect on the movement.
Look at the code, this is the code for counting the number of times.

#include <stdio.h>
int hanoi(int n)
{
    
    
	if (n >= 2)
		return 2 * hanoi(n - 1) + 1;
	return 1;
}
int main(void)
{
    
    
	printf("Problem of Hanoi\n");
	printf("please input the number for problem:>\n");
	int n;
	scanf("%d", &n);
	printf("%d",hanoi(n));
	return 0;
}

This is the code for the compilation step

#include <stdio.h>
void move(char start,char end,int n)
{
    
    
	static int count = 0;
	count++;
	printf("NO.%d step,the %d moves from %c to %c\n", count, n, start, end);
}
void hanoi(int n,char pose1,char pose2,char pose3)
{
    
    
	if (n == 1)
		move(pose1, pose3, 1);//一个是特例,不存在中间位置
	else
	{
    
    
		hanoi(n - 1, pose1, pose3, pose2);//对应第一次移动N-1个金片
		move(pose1, pose3, n);//对应移动第N个金片
		hanoi(n - 1, pose2, pose1, pose3);//最后一次移动N-1个金片
	}
}
int main(void)
{
    
    
	
	char pose1 = 'A';//起始位置
	char pose2 = 'B';//中间位置
	char pose3 = 'C';//结束位置
	int n;
	scanf("%d", &n);
	hanoi(n, pose1, pose2, pose3);
	
	return 0;
}

When using recursion, understand it as a functional module , rather than analyze it step by step. Use its function to determine the termination condition and approach this condition at the same time.
Using recursive writing functions, calculations can be "easy".
Just to deepen your own understanding, be student-like, don't laugh.
If you have any questions, please give me some advice.

Guess you like

Origin blog.csdn.net/weixin_52199109/article/details/113003580