c语言实现Hanoi塔移动步骤

 
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  * =  ( 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   * =  s -> pStub;

    
while ( p  !=  s -> pTop )
    {
        
/* this is for output */
        fprintf(fp,
"  %d " * (p));

        printf(
"  %d " * (p ++ ));
    }
    printf(
" " );

    
/* this is for output */
    fprintf(fp,
" " );

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值