[C language] Detailed explanation of the Tower of Hanoi problem (C language recursive function)

Problem introduction and background

Tower of Hanoi, also known as the Tower of Hanoi. It is an educational toy originating from ancient Indian legends. When Brahma created the world, he made three diamond pillars. On one pillar, there were 64 gold discs stacked in order of size. Brahma ordered Brahmin to rearrange the discs on another pillar in order of size from the bottom up. . It is also 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.
Next, we will analyze the specific ideas of the Tower of Hanoi problem!

Illustration of the Tower of Hanoi moving

n=3

Insert image description here

It can be understood here that we first put the previousn-1The two discs are moved to pillar B with the help of pillar C, then the largest disc is moved to pillar C, and then the same idea is executed.

n=4

Insert image description here
whenn=4, we can still first put the previousn-1a disc (here i.e.first three discs) try to place it on the B-pillar (the specific operation will be explained later), and then place the largest disk on the C-pillar. After doing this,Treat the largest disk as fixedand got the sumn=3A similar situation is shown in the figure above. Subsequent transfers are easyn=3same.

Problem analysis and code implementation

First n-1 disk movement method

Premise: There are n disks arranged on pillar A in order from small to large. There are three pillars. We mark these three pillars as A, B, and C respectively.
1.When n is an even number, move the smallest disk in the order of A->B->C->A, move once;When n is an odd number, move the smallest disk in the order of A->C->B->A, move once;
2. Then, move the movable disks on the other two pillars to the new pillar.That is, move the disc on the non-empty pillar to the empty pillar. When both pillars are non-empty, move the smaller disc., move once. Although this step does not clearly specify which disk to move, the action performed is unique.
3. Repeat operations 1 and 2, and finally complete the movement of the Tower of Hanoi.

Number of moves

Order Number of moves law
1 1 2^1-1
2 3 2^2–1
3 7 2^3-1
4 15 2^4-1
n \ 2^n-1

If we will move beforen-1According to the Hanoi(n-1)above conclusion, the number of moves of n disks can be Hanoi(n)=2*Hanoi(n-1)-1calculated using (n>0).

source code

int Hanoi(int n)
{
    
    
	if (n > 0)
		return 2 * Hanoi(n - 1);
	else
		return 1;
}
int main()
{
    
    
	int n = 0;
	printf("圆盘的个数:>");
	scanf("%d", &n);
	int ret = Hanoi(n) - 1;
	printf("%d\n", ret);
	return 0;
}

Insert image description here

Mobile step printing

To print out the moving steps in code, we must understand the entire process. In fact, there is a loop in the Tower of Hanoi movement:When n is an even number, it always loops with A->B, A->C, B->C, A->B, C->A, C->B; when n is an odd number, it always loops with A ->C,A->B,C->B,A->C,B->A,B->C loop.
The specific code implementation is as follows

#include<stdio.h>
#include<windows.h>
int count;
void Move(int n, char a, char b)
{
    
    
    count++;
    Sleep(1000);
    printf("第%2d次移动 Move %d: %c -> %c \n", count, n, a, b);
}
void Hanoi(int n, char a, char b, char c)
{
    
    
    if (n == 1)
    {
    
    
        Move(n, a, c);
    }
    else
    {
    
    
        Hanoi(n - 1, a, c, b);
        Move(n, a, c);
        Hanoi(n - 1, b, a, c);
    }
}

int main()
{
    
    
    int n = 0;
    printf("汉诺塔的层数:>");
    scanf(" %d", &n);
    Hanoi(n, 'A', 'B', 'C');
    return 0;
}

Insert image description here

Guess you like

Origin blog.csdn.net/2301_77404033/article/details/132132051
Recommended