Algorithm - Recursive Implementation of The Tower of Hanoi

Table of contents

introduction:

analyze:

Analyze the migration process of the two Towers of Hanoi:

 Analyze the migration process of the three Towers of Hanoi:

Code:

Recursive exit:

Recursive process: 

Complete program code:

operation result:

References:​​​​


 

introduction:

Today I came into contact with a very interesting game called Tower of Hanoi. I never played this puzzle game when I was a child, so today I use the code to realize this puzzle game. Before writing the code of the Tower of Hanoi, we first understand the game rules, gameplay, and game process of the Tower of Hanoi:

The Tower of Hanoi, also known as the Tower of Hanoi , is an educational toy that originated from an ancient Indian legend . When Brahma created the world, he made three diamond pillars. On one pillar, 64 gold discs were stacked in order of size from bottom to top. Brahma ordered Brahmin to rearrange the discs on another pillar in order of size from below. And it is stipulated that the disk cannot be enlarged on the small disk, and only one disk can be moved between the three pillars at a time.

analyze:

Analyze the migration process of the two Towers of Hanoi:

Suppose we now have two sheets on pillar A, blue square frame and red reverse frame, this is their initial state:

​​​​​​​cc54b2a5a80e43d2a7fdc56323a8d4bc.png 

We are now going to migrate the red and blue boxes from pillar A to pillar C according to the original "big one on the bottom and small one on top" rule. Obviously, we need to migrate the blue box to pillar B first, and then Migrating the red box to pillar C, and then migrating the blue box from pillar B to pillar C completes the migration.

Assuming that we migrate the topmost box of the pillar each time, the order of migration can be expressed as:

A -> B

06411c7213d842228b36f61d043a5546.png

A -> C

38ac9c3eb0f64e4aa22c973f2862c6b7.png

B -> C

984690a71f6f4ad3bd71d6d34e58ea37.png

So far, the two towers of Hanoi have been relocated.

 Analyze the migration process of the three Towers of Hanoi:

Suppose we want to migrate 3 Towers of Hanoi, which are red boxes, blue boxes, and yellow boxes. This is their initial state:

d1ec7b0d2039426491580c90b14a3985.png

Following the rules of the uppermost box of each migration pillar above, the sequence can be expressed as:

A -> C

f2d051e44a9b4738ab529fd329129c7f.png

A -> B

49c7a31b83374813ad1ac649bc8aa60b.png

C -> B

02d14875725948798016d95945bc4a87.png

A -> C

e87ed5bfceff47249a779301c155b552.png

B -> A

c1a1a84b037549c18b012bc1ee609163.png

B -> C

14913f231af44029aa9f059e71c2c79e.png

A -> C 

bcd7bf9f8c32419c95b08c532ba08186.png

So far, the three Towers of Hanoi have been relocated.

Then we should think next, what rules can we draw from two Hanoi Towers to three Hanoi Towers? 

We found that from the perspective of the migration process of the three Towers of Hanoi, what we need first is to migrate the boxes above the bottom to the B pillar first, release and migrate the largest box at the bottom to the C box, and then move the The two upper boxes are placed on top of the largest box in turn. In other words, no matter how many towers of Hanoi, we can transform a big problem into a small problem.

For example, if I want to migrate four pieces of the Tower of Hanoi, the first thing I need to consider is how to correctly migrate the three pieces above the bottom frame and migrate the bottom frame to the target pillar;

Then when I migrate the three Towers of Hanoi, what I have to consider is whether to correctly migrate the two pieces above the bottom frame and migrate the bottom frame to the target pillar;

Similarly, when I migrated two Towers of Hanoi, of course I migrated the upper one to the B pillar other than the target pillar, then moved the bottom frame to the target pillar, and then continued to migrate the frame of the B pillar to The C pillar thus completes the entire migration process.

We can use a dendrogram to simulate the process of gradually decomposing the big problem of four Hanoi towers into three and two Hanoi towers:

0e7aefc462b74728b73a44cea9370ef0.png

Note: 2, 3, and 4 in the merging process here represent the bottom boxes corresponding to each step. 

Code:

After the above analysis of the migration process of the Tower of Hanoi, we have a clear idea of ​​the general idea, and now we recursively implement the code of the Tower of Hanoi:

First of all, let's design a recursive function. The Tower of Hanoi problem involves nothing more than: the slices on the Tower of Hanoi and the three pillars (A, B, and C), so we use these two things as the function Formal parameters:

void Hanoi(int n, char A , char B , char C);

Recursive exit:

Since we want to recurse, the first thing we should consider is the recursive exit, that is, when the number of slices on the tower to be migrated is 1, we directly migrate this piece from the pillar to be migrated to the target pillar, which can be expressed in code that is:

if(n == 1){
        printf("%c -> %c\n",A,C);
    }

Recursive process: 

We assume that there are n slices of the Tower of Hanoi to be migrated. According to the decomposition and merging process analyzed above, the first thing we need to do is to migrate all the upper frames except the bottom frame to the B pillar, that is, transfer n - 1 box bypasses pillar C and migrates to pillar B, expressed in code:

Hanoi(n - 1,'A','C','B');

When we have successfully migrated n - 1 boxes to pillar B, the next thing we should consider is to migrate the underlying box numbered n to the target pillar (pillar C), that is, A -> C, using code to That means:

printf("%c -> %c\n",'A','C');

The next thing we should consider is to migrate the boxes on pillar B to pillar C in turn, that is, to migrate n - 1 pieces bypassing pillar A to pillar C, expressed in code:

Hanoi(n - 1,'B','A','C');

So far, the Tower of Hanoi with n squares has been migrated.

Complete program code:

#include<stdio.h>
void Hanoi(int n,char A,char B,char C)
{
    if(n == 1){
        printf("%c -> %c\n",A,C);
    }
    else{
        Hanoi(n - 1 , A,C,B);
        printf("%c -> %c\n",A,C);
        Hanoi(n - 1,B,A,C);
    }
}
int main()
{
    int n = 0;
    printf("Please enter the number of slice :");
    scanf("%d",&n);
    Hanoi(n,'A','B','C');
    return 0;
}

operation result:

For example, here I enter the number of boxes in the Tower of Hanoi as 3:

4b63ab275d3c4fcf99dd6cc7a244df30.png

The migration process of the running results is exactly the same as the migration process of the three Towers of Hanoi we analyzed above, and the result is correct. 

References:​​​​​​​​​​​​

Baidu Encyclopedia- Tower of Hanoi

 

Guess you like

Origin blog.csdn.net/weixin_45571585/article/details/126786782