Recursive algorithm analysis of Hanoi tower problem:
There are three pillars in a temple, and the first one has 64 plates, and the plates are getting bigger and larger from the top down. Ask the old monk in the temple to move all 64 plates to the third pillar. When moving, there is always a small plate pressing on a large plate. And you can only move one at a time.
1, at this time the old monk (later we call him the first monk) find it difficult, so he thought: if there is a person can move the first 63 plates to the second pillar, I will then move the last plate directly to the third pillar, and then let the person to move the first 63 plates from the second column on the third pillar, My task is complete, simple. So he looked for a younger monk than him (we called him a second monk later) and ordered:
①, you move the first 63 plates to the second pillar.
② and then I moved the 64th plate to the third pillar myself.
③, you move the first 63 plates to the third pillar.
2, the second monk took the task, also found it difficult, so he also and the first monk like: if there is a person can move the first 62 plates to the third pillar, I then the last plate to move directly to the second pillar, and then let the man to the first 62 plates from the third pillar moved to the third pillar, My task is complete, simple. So he also looked for a younger monk than him (we called him the third monk) and ordered:
①, you move the first 62 plates to the third pillar.
② and then I moved the 63rd plate to the second pillar myself.
③, you move the first 62 plates to the second pillar.
3, the third monk took the task, and the task of moving the first 61 plates of gourd words to the ladle to the fourth monk, and so on, until the task handed down to the 64th monk (estimated 64th monk very depressed, no chance also ordered others, because to him here the plate has only one).
4, to the completion of this task, to the completion of their respective duties. Complete the push back:
The 64th monk moves the 1th plate, moves it away, and the 63rd monk moves the 2nd plate he assigns to himself.
The 64th Monk then moved the 1th plate to the 2nd plate. By the completion of the 64th Monk's mission, the 63rd Monk completed the first step of the 62nd Monk's assignment to him.
From the above can be seen, only the 64th Monk's task completed, the 63rd Monk's task can be completed, only 2nd monk----64th Monk's task completed, 1th a monk's task to complete. This is a typical recursive problem. Now we have 3 plates to analyze:
1th Monk Order:
① 2nd Monk You first move the first 2 plates to the second pillar. (with the help of the third pillar)
② 1th Monk I moved the last plate of the first pillar to the third pillar myself.
③ 2nd Monk You move the first 2 plates from the second pillar to the third pillar.
Obviously, the second step is easy to achieve (eh, people are always selfish, the simple left to themselves, difficult to others).
One of the first steps, the 2nd monk he had 2 plates, he ordered:
① 3rd Monk You move the 1th plate of the first pillar to the third pillar. (with the help of the second pillar)
② 2nd Monk I moved the 2nd plate of the first pillar to the second pillar myself.
③ 3rd Monk You move the 1th plate from the third pillar to the second pillar.
Similarly, the second step is easy to achieve, but the 3rd monk he only needs to move 1 plates, so he does not have to send a mission. (Note: This is the condition for stopping recursion, also called the boundary value)
The third step can be broken down into, the 2nd monk still has 2 plates, command:
① 3rd Monk You move the 1th plate on the second pillar to the first pillar.
② the 2nd Monk. I moved the 2nd plate from the second pillar to the third one.
③ 3rd Monk You move the plate on the first pillar to the third pillar.
The analysis is composed of:1→3 1→2 3→2 move to the second pillar with a third pillar | 1→3 from a Private life | 2→1 2→3 1→3 move to the third pillar with the first pillar | A total of seven steps are required.
If it is 4 plates, then the first Monk's order of the 1th and 3rd steps each have 3 plates, so each need 7 steps, a total of 14 steps, plus the 1th monk 1 steps, so 4 plates in total need to move 7+1+7=15 step, the same, 5 plates need 15+1+15=31 step, 6 plates need 31+1 +31=64 Step ... As a result, it is necessary to move n plates (2 n times)-1 steps.
From the above comprehensive analysis, we know to move n plates from 1 seats (rather the first pillar) to 3 seats (rather the third pillar):
(1) Move the 1-seat (N-1) plate with 3 seats to 2 seats.
(2) Move the nth plate of the 1 seats to 3 seats.
(3) 2 seats (n-1) on the plate with the use of 1 mobile 3 seats.
Use Hanoi (N,A,B,C) below to move the 1-seater n plates to 3 seats with 2 seats.
It is obvious: (1) Step is Hanoi (n-1,1,3,2)
(3) Step is Hanoi (n-1,2,1,3)
In C language, that is:
#include <stdio.h>
int method (int n,char A, char b)
{
printf ("number.. %d.. Form: %c.. To.. %c: " n ", n,a,b);
return 0;
}
int Hanoi (int n,char A,char B,char c)
{
if (n==1) move (1,A,C);
Else
{
Hanoi (N-1,A,C,B);
Move (N,A,C);
Hanoi (N-1,B,A,C);
};
return 0;
}
int main ()
{
int num;
scanf ("%d", &num);
Hanoi (num, ' A ', ' B ', ' C ');
return 0;
}
Analysis of recursive algorithm for Hanoi tower problem