Tower of Hanoi Algorithm----C++ language recursive implementation

origin

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 which 64 golden discs were stacked in order from bottom to top. Brahma ordered the Brahmin to rearrange the disc on another pillar in order of size, starting from the bottom. And it is stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved at a time between the three columns.

abstraction as a math problem

As shown in the figure below, there are three pillars A, B, and C from left to right. There are n disks stacked from small to large on pillar A. It is now required to move the disk on pillar A to pillar C. During this period, only A principle: You can only move to one plate at a time and the large plate cannot be on the small plate. Find the steps and the number of moves.

Solution:
(1) n == 1

​The  1st disc A—->C sum = 1 time

(2) n == 2

​ 1st set A—->B
​ 2nd set No.  2 A—->C
​ 3rd set No. 1 B—->C sum = 3 times

(3)n == 3

​ 1st set No. 1 A—->C
​ 2nd set No. 2 A—->B
​ 3rd set No. 1 C—->B
​ 4th  set No. 3 A—->
C 5 times No. 1 disc B—->A
​ 6 times No. 2 disc B—->C
​ 7 times No. 1 disc A—->C sum = 7 times

It is not difficult to find the rules:

​ 1 disc to the power of 2 minus 1
​ 2 discs to the power of 2 minus 1
​ 3 discs to the power of 2 minus 1
​ . . . . .
​ n disks of degree 2 to the nth power minus 1

Therefore: the number of moves is: 2^n - 1

Algorithm Analysis (Recursive Algorithms)

When we use a computer to solve the Tower of Hanoi problem, an essential step is to perform an algorithmic analysis of the entire solution. So far, the simplest algorithm for solving the Tower of Hanoi problem is to solve the same recursion. As for what is recursion and what is the mechanism of recursive implementation, we are simply saying that it is a method or a function, but in There is a statement to call this function in my own function, and how can the call end? , there must also be an end point here, or specifically, the function can return a certain value after a certain call, and then the second-to-last can return a certain value, until the function is called for the first time can return a definite value.
The implementation of this algorithm can be simply divided into three steps:

(1) Move n-1 disks from A to B;
(2) Move the nth disk from A to C;
(3) Move n-1 disks from B to C;

Starting from here, and adding the analysis of the solution to the mathematical problem above, it is not difficult to find that the number of steps moved must be an odd number of steps:

(1) The middle step is to move the largest plate from A to C;
(2) Above the middle step, it can be seen that n-1 plates on A are moved to B by means of the auxiliary tower (tower C).
(3) Under the middle step, it can be seen that n-1 plates on B have been moved to C by means of the auxiliary tower (Tower A);

C++ source code

#include <iostream>

using namespace std;

void Move(int n, char A, char B, char C)
{
	if (n == 1)
	{
		//When there is only one disc, just move it from tower A to tower C
		cout << "move " << n << " from " << A << " to " << C << endl;
	}
	else
	{
		Move(n - 1, A, C, B);//Recursive, move the disk numbered 1~n-1 on the A tower to B, with C as the auxiliary tower
		cout << "move " << n << " from " << A << " to " << C << endl;//Move the disk numbered n on tower A to C
		Move(n - 1, B, A, C);//Recursive, move the disk numbered 1~n-1 on the B tower to C, with A as the auxiliary tower
	}
}

void Hanoi(int n)
{
	if (n <= 0)
		return;
	Move(n, 'A', 'B', 'C');
}

intmain()
{
	Hanoi(3);
	return 0;
}

Screenshot of program running

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811211&siteId=291194637