Recursive Algorithm 9-The Tower of Hanoi Problem of Complex Recursion

 

The Tower of Hanoi issue is derived from an ancient legend in India. When God created the world, he made three diamond pillars. On one pillar, 64 metal discs were stacked in order of size from top to bottom. God ordered the Brahman to re-place the disc on another pillar in order of size from below. It also stipulates that the disk cannot be enlarged on the small disk, and only one disk can be moved between the three pillars.

[Analysis]
This problem is actually to move n discs from pillar A to pillar C. Pillar B can be used during the movement. Only one disc can be moved at a time, and the big disc is always down and the small circle On the disk.

To move the n discs from pillar A to C with the aid of pillar B, first move the n-1 discs above from pillar A with pillar C to B, and then move the nth disc directly Go to the top of the pillar C, and then move the n-1 discs from the pillar B to the C with the aid of the pillar A, so that the problem of n is decomposed into the problem of n-1.

In this way, it is possible to move n plates from pillar A to pillar C, but there is still a problem that has not been solved, that is how to move n-1 discs from A to B, and then from pillar B to pillar C on.

To move n-1 discs from pillar A to pillar B, you need to absorb the n-2 discs above to move from pillar A to pillar C with the help of B, and then move the n-1 disc directly to B Then use A to move n-2 discs from C to B.

To move n-1 discs from column B to column C, recursion is needed. The process of moving the disc just conforms to the characteristics of recursion, reducing the larger problem to a smaller one. The condition for the end of the recursion is that only one plate needs to be moved at a time, otherwise the recursion will continue.

In order to simplify the problem, let's analyze the process of moving the three discs from column A to C by means of B.

(1) Move the two discs on column A to column B (with the help of column C).
(2) Move a disc on column A directly to column C (A->C).
(3) Move the two discs on column B to column C (with the help of column A).

Among them, step (2) can be implemented directly, and step (1) can continue to be decomposed.

a. Move a disc on column A directly to column C (A->C).
b. Move a disc on column A directly to column B (A->B).
c. Move a disc on column C directly to column B (C->B).

Step (3) can continue to decompose:

a. Move a disc on column B directly to column A (B->A).
b. Move a disc on column B directly to column C (B->C).
c. Move a disc on column A directly to column C (A->C).

A->C,A->B,C->B,B->A,B->C,A->C

code:

#include<stdio.h>
#include <iostream>
void hanoi(int n, char one, char two, char three);
void move(char x, char y);
void main()
{
	int n;
	printf("请输入圆盘的个数:");
	scanf("%d", &n);
	printf("移动步骤如下:\n");
	hanoi(n, 'A', 'B', 'C');
	system("pause");
}
void move(char x, char y)
{
	printf("%c-->%c\n", x, y);
}
void hanoi(int n, char one, char two, char three)
{
	if (n == 1)
		move(one, three);
	else
	{
		hanoi(n - 1, one, three, two);
		move(one, three);
		hanoi(n - 1, two, one, three);
	}
}


result:

 

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/104145360