Hanoi
塔问题
汉诺塔(又称河内塔)问题是印度的一个古老的传说。开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒,第一根上面套着64个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从这根棒搬到另一根棒上,规定可利用中间的一根棒作为帮助,但每次只能搬一个,而且大的不能放在小的上面。 (引自百度百科
http://baike.baidu.com/view/191666.htm)
#include
<
stdio.h
>
#include < stdlib.h >
struct IntStack
{
int * pStub;
int * pTop;
int capability;
};
Destination( int dishCount, char source, char dest, char helper);
Action( int dishNum, char source, char dest);
int CreateIntStack( int capability);
Pop( struct IntStack * s);
Push( struct IntStack * s, int value);
FreeIntStack( struct IntStack * s);
PrintIntStack( struct IntStack * s);
struct IntStack * pA, * pB, * pC;
int step = 0 ;
/* this is for output to file */
FILE * fp;
main()
{
int dishCount = 3 ;
int i;
/* this is for output to file */
if ( (fp = fopen( " d:/hanoioutput.txt " , " wt+ " )) == NULL )
{
printf( " open file error! " );
getch();
exit( 1 );
}
pA = ( struct IntStack * )CreateIntStack(dishCount);
pB = ( struct IntStack * )CreateIntStack(dishCount);
pC = ( struct IntStack * )CreateIntStack(dishCount);
if ( pA == NULL || pB == NULL || pC == NULL)
{
printf( " Create Stack error! " );
getch();
return ;
}
for (i = dishCount; i > 0 ; i -- )
{
Push(pA, i);
}
PrintIntStack(pA);
PrintIntStack(pB);
PrintIntStack(pC);
Destination(dishCount, ' A ' , ' C ' , ' B ' );
printf( " hanoi resolved! " );
FreeIntStack(pA);
FreeIntStack(pB);
FreeIntStack(pC);
getch();
}
/*
dishCount: the count of dishes.
source: dish's place.
dest: place where dishes will be moved to.
helper: the help place.
*/
Destination( int dishCount, char source, char dest, char helper)
{
if ( dishCount > 1 )
{
Destination(dishCount - 1 , source, helper, dest);
Action(dishCount, source, dest);
Destination(dishCount - 1 , helper, dest, source);
}
else
{
Action(dishCount, source, dest);
}
}
/*
dishNum: the dish that will be moved.
source: dish's place.
dest: palce where the dish will be moved to.
*/
Action( int dishNum, char source, char dest)
{
if ( source != ' A ' && source != ' B ' && source != ' C ' )
{
printf( " para source error! " );
return ;
}
if ( dest != ' A ' && dest != ' B ' && dest != ' C ' )
{
printf( " para dest error! " );
return ;
}
printf( " step:%d dish:%d %c--->%c " , ++ step, dishNum, source, dest);
/* this is for output to file */
fprintf(fp, " step:%d dish:%d %c--->%c " , step, dishNum, source, dest);
if ( source == ' A ' )
{
Pop(pA);
}
else if ( source == ' B ' )
{
Pop(pB);
}
else if ( source == ' C ' )
{
Pop(pC);
}
if ( dest == ' A ' )
{
Push(pA, dishNum);
}
else if ( dest == ' B ' )
{
Push(pB, dishNum);
}
else if ( dest == ' C ' )
{
Push(pC, dishNum);
}
PrintIntStack(pA);
PrintIntStack(pB);
PrintIntStack(pC);
}
int CreateIntStack( int capability)
{
struct IntStack * s = ( struct IntStack * )malloc( sizeof ( struct IntStack) );
int * p = ( int * )calloc( sizeof ( int ), capability + 1 );
s -> pStub = & p[ 0 ];
s -> pTop = & p[ 0 ];
s -> capability = capability;
return ( int ) s;
}
Pop( struct IntStack * s)
{
if ( s -> pTop - s -> pStub > 0 )
{
s -> pTop -- ;
}
else
{
printf( " on data to pop out! " );
}
}
Push( struct IntStack * s, int value)
{
if ( s -> pTop - s -> pStub >= s -> capability )
{
printf( " stack is full! " );
}
else
{
* (s -> pTop) = value;
s -> pTop ++ ;
}
}
FreeIntStack( struct IntStack * s)
{
if ( s -> pStub != NULL )
{
free(s -> pStub);
}
if ( s != NULL)
{
free(s);
}
s = NULL;
}
PrintIntStack( struct IntStack * s)
{
int * p = s -> pStub;
while ( p != s -> pTop )
{
/* this is for output */
fprintf(fp, " %d " , * (p));
printf( " %d " , * (p ++ ));
}
printf( " " );
/* this is for output */
fprintf(fp, " " );
}
#include < stdlib.h >
struct IntStack
{
int * pStub;
int * pTop;
int capability;
};
Destination( int dishCount, char source, char dest, char helper);
Action( int dishNum, char source, char dest);
int CreateIntStack( int capability);
Pop( struct IntStack * s);
Push( struct IntStack * s, int value);
FreeIntStack( struct IntStack * s);
PrintIntStack( struct IntStack * s);
struct IntStack * pA, * pB, * pC;
int step = 0 ;
/* this is for output to file */
FILE * fp;
main()
{
int dishCount = 3 ;
int i;
/* this is for output to file */
if ( (fp = fopen( " d:/hanoioutput.txt " , " wt+ " )) == NULL )
{
printf( " open file error! " );
getch();
exit( 1 );
}
pA = ( struct IntStack * )CreateIntStack(dishCount);
pB = ( struct IntStack * )CreateIntStack(dishCount);
pC = ( struct IntStack * )CreateIntStack(dishCount);
if ( pA == NULL || pB == NULL || pC == NULL)
{
printf( " Create Stack error! " );
getch();
return ;
}
for (i = dishCount; i > 0 ; i -- )
{
Push(pA, i);
}
PrintIntStack(pA);
PrintIntStack(pB);
PrintIntStack(pC);
Destination(dishCount, ' A ' , ' C ' , ' B ' );
printf( " hanoi resolved! " );
FreeIntStack(pA);
FreeIntStack(pB);
FreeIntStack(pC);
getch();
}
/*
dishCount: the count of dishes.
source: dish's place.
dest: place where dishes will be moved to.
helper: the help place.
*/
Destination( int dishCount, char source, char dest, char helper)
{
if ( dishCount > 1 )
{
Destination(dishCount - 1 , source, helper, dest);
Action(dishCount, source, dest);
Destination(dishCount - 1 , helper, dest, source);
}
else
{
Action(dishCount, source, dest);
}
}
/*
dishNum: the dish that will be moved.
source: dish's place.
dest: palce where the dish will be moved to.
*/
Action( int dishNum, char source, char dest)
{
if ( source != ' A ' && source != ' B ' && source != ' C ' )
{
printf( " para source error! " );
return ;
}
if ( dest != ' A ' && dest != ' B ' && dest != ' C ' )
{
printf( " para dest error! " );
return ;
}
printf( " step:%d dish:%d %c--->%c " , ++ step, dishNum, source, dest);
/* this is for output to file */
fprintf(fp, " step:%d dish:%d %c--->%c " , step, dishNum, source, dest);
if ( source == ' A ' )
{
Pop(pA);
}
else if ( source == ' B ' )
{
Pop(pB);
}
else if ( source == ' C ' )
{
Pop(pC);
}
if ( dest == ' A ' )
{
Push(pA, dishNum);
}
else if ( dest == ' B ' )
{
Push(pB, dishNum);
}
else if ( dest == ' C ' )
{
Push(pC, dishNum);
}
PrintIntStack(pA);
PrintIntStack(pB);
PrintIntStack(pC);
}
int CreateIntStack( int capability)
{
struct IntStack * s = ( struct IntStack * )malloc( sizeof ( struct IntStack) );
int * p = ( int * )calloc( sizeof ( int ), capability + 1 );
s -> pStub = & p[ 0 ];
s -> pTop = & p[ 0 ];
s -> capability = capability;
return ( int ) s;
}
Pop( struct IntStack * s)
{
if ( s -> pTop - s -> pStub > 0 )
{
s -> pTop -- ;
}
else
{
printf( " on data to pop out! " );
}
}
Push( struct IntStack * s, int value)
{
if ( s -> pTop - s -> pStub >= s -> capability )
{
printf( " stack is full! " );
}
else
{
* (s -> pTop) = value;
s -> pTop ++ ;
}
}
FreeIntStack( struct IntStack * s)
{
if ( s -> pStub != NULL )
{
free(s -> pStub);
}
if ( s != NULL)
{
free(s);
}
s = NULL;
}
PrintIntStack( struct IntStack * s)
{
int * p = s -> pStub;
while ( p != s -> pTop )
{
/* this is for output */
fprintf(fp, " %d " , * (p));
printf( " %d " , * (p ++ ));
}
printf( " " );
/* this is for output */
fprintf(fp, " " );
}