Tower of Hanoi, easy to understand

Tower of Hanoi

There are three pins on a board, A, B, C. There are 64 discs of different sizes on the needle A, with the larger one on the bottom and the small one on the top. There is an old monk who wants to move these 64 discs from needle A to needle C. Only one disc can be moved at a time, and the movement can be carried out with the help of needle B. But at any time, any disc on the needle must keep the big disc down and the small disc up. Find the steps to move.

The idea of ​​problem solving is like this

First of all, the old monk thinks about it this way. If someone can help me move the 63 plates above from one to another, the problem will be solved. The old monk only needs to do at this time:

  • Order the second monk to move 63 plates from A to B
  • Move the bottom plate from A to C by yourself
  • Then order the second monk to move 63 plates from B to C

Then the second monk found will figure out how to move 63 plates from A to B, and then find another monk to help. What the second monk needs to do at this time is:

  • Order the third monk to move 62 plates from A to C
  • Move 1 plate from A to B by yourself
  • Then order the third monk to move 62 plates from C to B

After performing a recursive process, we will find the 63rd monk and let him finish moving the two plates from one to the other (because the initial position that needs to be moved will change every time the move starts, so here I am also Not sure), finally found the 64th monk, let him finish moving a plate from one to another, the work is completely over.

Algorithm analysis of this question

Suppose there are n plates on A.
If n=1, move the disc from A to C directly.
If n=2, then:
1. Move n-1 (equal to 1) discs on
A to B; 2. Move a disc on A to C;
3. Finally, move the disc on B n-1 (equal to 1) discs are moved to C.
If n=3, then:
A. Move n-1 (equal to 2, let it be n )个圆盘移到 B(借助于 C),步骤如下: (1)将 A 上的 n-1 (equal to 1) discs on A to C.
(2) Move a disc on A to B.
(3) Move the n -1(等于 1)个圆盘移到 B。 B. 将 A 上的一个圆盘移到 C。 C. 将 B 上的 n-1(等于 2,令其为 n) discs on C to C (with A), the steps are as follows:
(1) Move n -1(等于 1)个圆盘移到 A。 (2)将 B 上的一个盘子移到 C。 (3)将 A 上的 n-1 (equal to 1) discs on B to C.
At this point, the movement of the three discs is completed.
It can be seen from the above analysis that when n is greater than or equal to 2, the moving process can be decomposed into three steps: the
first step is to move n-1 discs on
A to B; the second step is to move one of A on The disk is moved to C; the
third step is to move the n-1 disks on B to C; the first and third steps are similar.
When n=3, the first and third steps are decomposed into three similar steps, that is, n -1 个圆盘从一个针移到另一 个针上,这里的 n=n-1. Obviously this is a recursive process, according to which the algorithm can be programmed as follows

#include<stdio.h>
void hanoi(int n,char A,char B,char C){
    
    //找到第一个和尚,他需要做的A->B
	if (n == 1){
    
    //别人帮第一个和尚把除了最底下的一个所有都移好了之后,
		//      自己把最底下的那个放过去
		printf("Move %d:from %c to %c\n",n,A,B);
	}
	else{
    
    
		hanoi(n - 1, A, C, B);//第一个和尚找到第二个和尚,他需要A->C,
		                      //这个时候函数参数的位置发生了变化
		printf("Move %d:from %c to %c\n", n, A, B);
		hanoi(n-1,C,B,A);//第二个和尚再把C->B
	}
}
void main(){
    
    
	int n;
	char A = 'A';
	char B = 'B';
	char C = 'C';
	printf("输入几个圆盘:");
	scanf("%d", &n);
	printf("A座到B座\n");
	hanoi(n, A, B, C);
}

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/109903810