Solve the Tower of Hanoi Problem with Function Recursion

There is a classic example of the use of function recursion algorithm, that is, the Tower of Hanoi problem. Next, let us take a look at how to use function recursion to solve the Tower of Hanoi problem!
The origin of the Tower of Hanoi issue: The issue of the
Tower of Hanoi (also known as the Tower of Hanoi) is an ancient legend in India. Brahma, the pioneering god, left three diamond rods in a temple. The first one is covered with 64 round gold pieces, the largest one is underneath, and the other one is smaller than one. Stack them one by one. The monks tirelessly moved them one by one from one stick to another. It is stipulated that the middle stick can be used as help, but only one can be moved at a time, and the big one cannot be placed on top of the small one. . Facing the huge number (number of times to move the disc) 18446744073709551615, it seems that the monks are unable to complete the movement of the gold pieces after exhausting their entire lives. Later, this legend evolved into the Tower of Hanoi game.
The rules of the Tower of Hanoi:
Simply put, all n discs on the a-pillar are transported to the c-pillar through the b-pillar as an aid. In the process of handling, only one disc can be carried at a time, and the large disc cannot be placed on the small disc.
Insert picture description here
What is recursion?
To use recursion to solve this problem, we must first know what recursion is.
Recursion is when a function calls itself in its function body. Executing a recursive function will call itself repeatedly, and enter a new layer each time it is called. The recursive function must have an end condition.
When the function keeps recursing until it returns after encountering a wall, this wall is the end condition.
So recursion must have two elements, the end condition and the recurrence relationship.
Problem analysis:
It is easy for us to know:
when n=1, we directly move the plate from column a to column c to solve the problem.
When n=2, we can first move the first plate on the a pillar to the b pillar (the number of the top plate on the a pillar is 1, and it will increase sequentially), and then move the second plate on the a pillar Go to the c-pillar, and then move the plate on the b-pillar to the c-pillar.
But when the value of n is 3 and above, the problem becomes troublesome. So can we reduce the latter complex problem to the first two simple cases?
The answer is yes, and this is also the purpose of function recursion: to simplify complex problems.
Code implementation:
We only need to divide the number of plates into two categories to solve:
when n=1, change the plate from a->c.
When n>1, move the n-1 plates on the a pillar to the b pillar, and then move the remaining one (that is, the nth) on the a pillar to the c pillar, and then move the Move n-1 to the c-pillar. (Remember that we are looking at n-1 plates as a whole here)
Insert picture description here
Insert picture description here

Among them, void move(int n, char a, char b, char c) means that you want to move n plates from the a pillar through the b pillar to the c pillar. (The corresponding operation is realized through the number or character you pass in) After the
programming is completed, we can enter a number that is easier to detect to see if the program is correct.
Insert picture description here

Personal opinion:
In my opinion, the Tower of Hanoi problem, including most problems that can be solved by function recursion, requires our ability to treat some things as a whole. For example, in the Tower of Hanoi problem, we have to consider n-1 as a whole. If we cannot achieve this understanding, it will be difficult to understand how to solve the Tower of Hanoi problem with function recursion.

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/112756382