Hanoi's recursive implementation algorithm, the disc A in the use of B disk completely moved to the C disk,
Only one disc can be moved at a time, and the market cannot be placed on a small disk each time it moves
The pseudo algorithm for recursive functions is as follows:
if (n = = 1)
Move the disc on column A directly from A to C
Else
First, the N-1 disc on column A is moved to Pillar B with the help of column C.
Move the nth disc directly on column A to pillar C.
Finally, the n-1 disc on pillar B is moved to pillar C with the help of a pillar.
The time complexity of the recursive algorithm is O (2 n-th side), when there are N-disk, you need to move the disk 2 of the n-th-1 times
Operating system: Ubuntu
Compiling software: GCC
Results screenshot:
Source:
#include <stdio.h>
void Move (Int,char,char,char);
int main ()
{
//a, B, C, respectively, represents three columns
char ch1 = ' A ';
Char CH2 = ' B ';
char CH3 = ' C ';
n represents the number of disks
int n;
printf ("Please enter the number of discs:");
scanf ("%d", &n);
Move (N,CH1,CH2,CH3);
return 0;
}
To move n discs from X pillars to Z pillars with the y pillar
void motion (int n,char X,char Y,char z)
{
if (n = 1)
printf ("Disk number%d: Moving from%c to% C\n ", n,x,z);
else
{move
(n-1,x,z,y);
printf ("Disk number%d: Moving from%c to%c\n", n,x,z);
Move (n-1,y,x,z);
}
}