The source of the program is to refer to the Yin Cheng Teacher's instructional video and practice to get the results.
SOURCE program for Hanoi:
#include <stdio.h>
#include <stdlib.h>
#define N 20
int a[n][3] = {0};
int count = 0;//counter
void Show (int a[n][3]);//Display function
void Hanoi (int n, char A, Char B, char C);//move process, control
void Move (char X, char Y);//move, connect data, view
void Show (int a[n][3])
{
printf ("%c\t%c\t%c\t\n", ' A ', ' B ', ' C ');
printf ("*********************\n");
for (int i = 0; I <n; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf ("%d\t", A[i][j]);
}
printf ("\ n");
}
}
void Hanoi (int n, char A, Char B, char C)
{
if (n < 1)
Return
else if (n = = 1)
{
printf ("\n%c->%c\n", A, C);//move directly
Move (A, C);
count++;
Show (a);
}
Else
{
Hanoi (n-1,a,c,b);//a->b C does the intermediate amount. Move all the n-1 to B.
printf ("\n%c->%c\n", A, C);//move directly
Move (A, C);
count++;
Show (a);
Hanoi (n-1, B, A, C);//Move All n-1 on the B to C, A is the middle
}
}
void Move (char X, char Y)
{
int m = X-65;
int n = y-65;//If Y is a C result of 2, indicates the number of columns. [N-1] N
int imove =-1;
for (int i = 0; i < N; ++i)
{
if (a[i][m]! = 0)
{
Imove = i;
break;//found the first non-0 data
}
}
A[IMOVE][M]
A[jmove][n]
int jmove;
if (a[n-1][n] = = 0)
Jmove = n-1;//last row
Else
{
Jmove = 0;
for (int i = 0; i < N; ++i)
{
if (a[i][n]! = 0)
{
Jmove = i;
Break
}
}
Jmove-= 1;//retrieving the first nonzero data
}
int temp = a[imove][m];
A[IMOVE][M] = A[jmove][n];
A[jmove][n] = temp;
}
void Main ()
{
int n;
printf ("Please input:\n");
scanf_s ("%d", &n);
for (int i = 0; i < n; ++i)
{
A[n-1-i][0] = n-i;
}
Create Hanoi
Show (a);
Hanoi (n, ' A ', ' B ', ' C ');
printf ("move altogether%d times \ n", count);
System ("pause");
}
A solution to the problem of Hanoi Tower