Tower of Hanoi---C code implementation and application of the power button

Tower of Hanoi

According to legend, a long time ago, a few monks in a temple moved 64 plates non-stop all day long, day after day, year after year. It is said that the day when all 64 plates are removed is the end of the world...

Problem description
There are three pillars A, B, and C. There are n plates on A. We want to move the plate on A to C, but the following three conditions must be met:

·You can only move one plate at a time;
·The plate can only slide out from the top of the column to the next pillar;
·The plate can only be stacked on a plate larger than it.

Insert picture description here

analysis:

This is a classic function recursion problem, and it is also an introductory topic for recursion. It is difficult to see through direct analysis, so we can analyze some simple situations first;
if the Tower of Hanoi has only three layers: the
first step is to put the first Put one from A to C, then put the second to B, then put the first from C to B, and then put the third to C at this time, and then put the first from B To A, put the second one to C and then the first one to C.
It is represented by a picture:
Insert picture description here

So it can be seen that the first step we need to complete this problem is to put all the layers above the last one on B, then extract the last one and put it on C, and then put the one on B on C.
The reason for this operation is because the last one on A must also be the last one on C.

Therefore, when there are 64 layers, we can first decompose this big problem into three steps:

1. Move the first 63 on A to B with the help of C.
2. Move the 64th on A directly to C.
3. Use A to C from 63 on B.

At this time, we were surprised to find that the original 64-layer problem was broken down into 63 layers .
And because our big problem is actually "move the first 64 on A to C with the help of B." Observing it from steps 1 and 3 above shows that the format is exactly the same, except that the number of layers, starting point, and ending point have changed. That is to say, it is not difficult to find that the separated steps 1 and 3 are actually a kind of Hanoi Tower problem , but the number of layers and the position of the needle have changed .

At this time, we can actually perform another decomposition observation on step 1:
It can also be divided into three steps:

1. Move the first 62 on A to C with B.
2. Move the 63rd on A directly to B.
3. Move 62 on C to B with A.

From this we can draw the law-that is to decompose according to this step, so that each separated step is an independent Tower of Hanoi problem! ! !
Therefore, the recursive method can be used to solve multiple times. That is to say, we will continue to decompose this problem first, until the decomposition can be directly "shifted", we then start to "shift".

C code:

#include<stdio.h>
void hanoi(int n,char A,char B,char C)//此为进行“挪移”的函数
{
    
    //需要注意的是这里的A,B,C并不是单纯的指原先题目给的A针B针C针,这里是A指起点,B为借助的点,C为移动到的终点
	if(n==1)
	{
    
    
		printf("%c --> %c\n",A,C);//一层时直接从起点挪移到终点(A,C不代表某根针)
	}
	else
	{
    
    
		hanoi(n-1,A,C,B);
		printf(" %c --> %c\n",A,C);//挪移操作,即原步骤2
		hanoi(n-1,B,A,C); 
	}
}
int main()
{
    
    
	int n;
	printf("Please enter the number of floors of Hanoi tower:\n");
	scanf("%d",&n);
	hanoi(n,'A','B','C');//先把初始顺序和初始层数放进去
	return 0;	
} 

In order to prevent confusing A, B, and C as the starting point, intermediate point, and ending point, I wrote another one:

#include<stdio.h>
void hanoi(int n,char start,char middle,char end)
{
    
    
	if(n==1)
	{
    
    
		printf("%c --> %c\n",start,end);
	}
	else
	{
    
    
		hanoi(n-1,start,end,middle);
		printf(" %c --> %c\n",start,end);
		hanoi(n-1,middle,start,end); 
	}
}
int main()
{
    
    
	int n;
	printf("Please enter the number of floors of Hanoi tower:\n");
	scanf("%d",&n);
	hanoi(n,'x','y','z');
	return 0;	
} 

Solve it in the same way in Likou—

Interview Question 08.06. The Tower of Hanoi Question

In the classic Tower of Hanoi problem, there are 3 pillars and N perforated discs of different sizes, and the plate can slide into any pillar. At the beginning, all the plates are placed on the first pillar in ascending order from top to bottom (that is, each plate can only be placed on a larger plate). The following restrictions apply when moving the disc:
(1) Only one plate can be moved at a time;
(2) The plate can only slide from the top of the column to the next column;
(3) The plate can only be stacked on a plate larger than it on.

Please write a program to use the stack to move all the plates from the first pillar to the last pillar.

You need to modify the stack in place.

Example 1:

Input: A = [2, 1, 0], B = [], C = []
Output: C = [2, 1, 0]
Example 2:

Input: A = [1, 0], B = [], C = []
Output: C = [1, 0]

提示:
A中盘子的数目不大于14个。

Code:

#define MAX_LEN 200
typedef struct {
    
    
    int* data;
    int size;
}Stack;
int cnt = 0;
void Push(Stack* s, int d)
{
    
    
    s->data[s->size++] = d;
}
int Pop(Stack* s){
    
    
    return s->data[--s->size];
}

void hannuota(int n, Stack* A, Stack* B, Stack* C){
    
    
    if(n == 1){
    
    
        cnt ++;
        Push(C, Pop(A));
        return;
    }
    hannuota(n-1, A, C, B);
    hannuota(1, A, B, C);
    hannuota(n-1, B, A, C);
}

void hanota(int* A, int ASize, int* B, int BSize, int** C, int* CSize){
    
    
    if(ASize <= 0) return;
    Stack X,Y,Z;
    X.data = A;
    X.size = ASize;
    Y.data = (int*)malloc(sizeof(int)*MAX_LEN);
    Y.size = 0;
    int *p = (int*)malloc(sizeof(int)*MAX_LEN);
    Z.data = p;
    Z.size = 0;
    hannuota(ASize, &X, &Y, &Z);
    *C = p;
    *CSize = Z.size;
    free(Y.data);
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/112239914