First on the effect chart
Again attach source code:
Hanoi
Copy Code code as follows:
#include "stdio.h"
#include "math.h"
int arra[15], arrb[15], arrc[15]; For a, B, C, respectively.
int length;
int LenA, LenB, Lenc;
Char plate[32];
Make
void makeplate (int n)
{
int i;
if (n = length + 1)
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
Plate[i] = ' _ ';
}
}
}
Else
{
if (n = = 0)
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
Plate[i] = ';
}
}
}
Else
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
if (i >= length + 1-n && i <= length | | i > length + 1
&& I <= length + 1 + N)
{
Plate[i] = ' _ ';
}
Else
{
Plate[i] = ';
}
}
}
}
}
Plate[i] = ' the ';
}
Draw
void Drawtower ()
{
int i;
printf ("");
for (i = length; I >= 0; i--)
{
if (i <= LenA)
{
Makeplate (Arra[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
if (i <= LenB)
{
Makeplate (Arrb[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
if (i <= lenc)
{
Makeplate (Arrc[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
printf ("\ n");
}
}
Move
void Moveplate (int n, char x, char y)
{
int I, J;
if (x = = ' A ')
{
lena--;
}
Else
{
if (x = = ' B ')
{
lenb--;
}
Else
{
lenc--;
}
}
if (y = = ' A ')
{
lena++;
Arra[lena] = n;
}
Else
{
if (y = = ' B ')
{
lenb++;
ARRB[LENB] = n;
}
Else
{
lenc++;
Arrc[lenc] = n;
}
}
Drawtower (); Draw the state of a Hanoi after moving once
}
Print and move
void Printandmove (int n, char x, char y)
{
printf ("\ n%d disk moves from%c to column%c \ n", n, x, y);
Moveplate (n, x, y);
}
Hanoi
void Hanoi (int n, char One, Char two, char three)
{
if (n = = 1)
{
Printandmove (n, one, three);
}
Else
{
Hanoi (N-1, one, three, two);
Printandmove (n, one, three);
Hanoi (N-1, two, one, three);
}
}
Main
void Main ()
{
int n, I; n is the number of Hanoi plate, if you want to change, just change the initial value.
char one = ' A ', two = ' B ', three = ' C ';
printf ("Please enter the number of plates [1-12]:");
scanf ("%d", &n);
if (n >= 1 && n <= 12)
{
length = n;
LenA = n;
for (i = 0; I <= lenA; i++)
{
Arra[i] = n + 1-i;
}
LenB = Lenc = 0;
Arrb[0] = Arrc[0] = n + 1;
printf ("Hanoi simulated move process [%d disk]\n\n", n);
Drawtower (); Draw the Hanoi initial state
Hanoi (N, one, two, three);
printf ("\ n analog end, move%ld \ n", (Long) POW (2, N)-1);
}
Else
{
printf ("Data Error!") \ n ");
}
}
Hanoi. C
Copy Code code as follows:
/* Hanoi Simulation
2013-5-13
*/
#include "stdio.h"
#include "math.h"
int arra[15], arrb[15], arrc[15]; For a, B, C, respectively.
int length;
int LenA, LenB, Lenc;
Char plate[32];
Make
void makeplate (int n)
{
int i;
if (n = length + 1)
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
Plate[i] = ' _ ';
}
}
}
Else
{
if (n = = 0)
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
Plate[i] = ';
}
}
}
Else
{
for (i = 0; i < 2 * length + 3; i++)
{
if (i = = length + 1)
{
Plate[i] = ' | ';
}
Else
{
if (i >= length + 1-n && i <= length | | i > length + 1
&& I <= length + 1 + N)
{
Plate[i] = ' _ ';
}
Else
{
Plate[i] = ';
}
}
}
}
}
Plate[i] = ' the ';
}
Draw
void Drawtower ()
{
int i;
printf ("");
for (i = length; I >= 0; i--)
{
if (i <= LenA)
{
Makeplate (Arra[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
if (i <= LenB)
{
Makeplate (Arrb[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
if (i <= lenc)
{
Makeplate (Arrc[i]);
printf ("%s", plate);
}
Else
{
Makeplate (0);
printf ("%s", plate);
}
printf ("\ n");
}
}
Move
void Moveplate (int n, char x, char y)
{
int I, J;
if (x = = ' A ')
{
lena--;
}
Else
{
if (x = = ' B ')
{
lenb--;
}
Else
{
lenc--;
}
}
if (y = = ' A ')
{
lena++;
Arra[lena] = n;
}
Else
{
if (y = = ' B ')
{
lenb++;
ARRB[LENB] = n;
}
Else
{
lenc++;
Arrc[lenc] = n;
}
}
Drawtower (); Draw the state of a Hanoi after moving once
}
Print and move
void Printandmove (int n, char x, char y)
{
printf ("\ n%d disk moves from%c to column%c \ n", n, x, y);
Moveplate (n, x, y);
}
Hanoi
void Hanoi (int n, char One, Char two, char three)
{
if (n = = 1)
{
Printandmove (n, one, three);
}
Else
{
Hanoi (N-1, one, three, two);
Printandmove (n, one, three);
Hanoi (N-1, two, one, three);
}
}
Main
void Main ()
{
int n, I; n is the number of Hanoi plate, if you want to change, just change the initial value.
char one = ' A ', two = ' B ', three = ' C ';
printf ("Please enter the number of plates [1-12]:");
scanf ("%d", &n);
if (n >= 1 && n <= 12)
{
length = n;
LenA = n;
for (i = 0; I <= lenA; i++)
{
Arra[i] = n + 1-i;
}
LenB = Lenc = 0;
Arrb[0] = Arrc[0] = n + 1;
printf ("Hanoi simulated move process [%d disk]\n\n", n);
Drawtower (); Draw the Hanoi initial state
Hanoi (N, one, two, three);
printf ("\ n analog end, move%ld \ n", (Long) POW (2, N)-1);
}
Else
{
printf ("Data Error!") \ n ");
}
}
The above is about VC + + implementation of the Hanoi effect of all the code, I hope to understand the Hanoi algorithm can help.