能实现汉诺塔游戏的基本即可
请发到[email protected]
立刻给分!

解决方案 »

  1.   


    #include <stdio.h>
    #include <alloc.h>
     
    #define N 5
    int direc;
    int tmp;
    /* 保存每次迭代的盘子数量 */
    struct numnode{
     int num;
     struct numnode *next; 
    };struct numnode *nn;
    /* 分别保存每次迭代的原柱,中间柱,目标柱的3个指针 */
    struct ptnode{
     char *ch;
     struct ptnode *next;
    } *s1,*s2,*s3;
    /* 原柱指针堆栈的push函数 */ 
    void push1(char* a){
     struct ptnode *tmp;
     tmp=(struct ptnode *)malloc((int)sizeof(struct ptnode));
     tmp->ch=a;
     tmp->next=s1;
     s1=tmp; 
    }
    /* 中间柱指针堆栈的push函数 */ 
    void push2(char* a){
     struct ptnode *tmp;
     tmp=(struct ptnode *)malloc((int)sizeof(struct ptnode));
     tmp->ch=a;
     tmp->next=s2;
     s2=tmp; 
    }
    /* 目标柱指针堆栈的push函数 */ 
    void push3(char* a){
     struct ptnode *tmp;
     tmp=(struct ptnode *)malloc((int)sizeof(struct ptnode));
     tmp->ch=a;
     tmp->next=s3;
     s3=tmp; 
    }
    /* 原柱指针堆栈的pop函数 */ 
    char* pop1(){
     struct ptnode *tmp;
     char* ch;
     ch=s1->ch; 
     tmp=s1;
     s1=s1->next;
     free(tmp);
     return ch;
    }
    /* 中间柱指针堆栈的pop函数 */ 
    char* pop2(){
     struct ptnode *tmp;
     char* ch;
     ch=s2->ch; 
     tmp= s2;
     s2=s2->next;
     free(tmp);
     return ch;
    }
    /* 目标柱指针堆栈的pop函数 */ 
    char* pop3(){
     struct ptnode *tmp;
     char* ch;
     ch=s3->ch; 
     tmp= s3;
     s3=s3->next;
     free(tmp);
     return ch;

    /* 保存盘子个数堆栈的push函数 */ 
    void pushn(int a){
     struct numnode *tmp;
     tmp=(struct numnode *)malloc((int)sizeof(struct numnode));
     tmp->num=a;
     tmp->next=nn;
     nn=tmp; 
    }
    /* 保存盘子个数堆栈的pop函数 */
    int popn(){
     struct numnode *tmp;
     int a;
     a=nn->num; 
     tmp= nn;
     nn=nn->next;
     /* free(tmp); */
     return a;

    /* 原柱,中间柱,目标柱初值数组 */ 
    char a[9]={'1','2','3','4','5','6','7','8','9'};char b[9]={'0','0','0','0','0','0','0','0','0'};char c[9]={'0','0','0','0','0','0','0','0','0'};int num=N;
    char *x1=a;
    char *x2=b;
    char *x3=c;
    int step=0; 
    void prnt();/*   核心函数,功能:进行迭代、回溯、移位操作    */
    /*   参数说明:n:盘子个数;                      */
    /*             t1,t2,t3:3指针                   */
    /*               分别指向原柱、中间柱、          */
    /*               目标柱数组;                    */  
    void digui(int n,char *t1,char *t2,char *t3){
        if(n<=0)return;
     if(direc>0){
      if(n>1){
        pushn(n);
        push1(t2);
        push2(t1);
        push3(t3);
        x1=t1;
        x2=t3;
        x3=t2;
        num--;
        return;
            }        
            t3[0]=t1[0];
            t1[0]='0';
            prnt();
            direc=-1;
            num=popn();
            x1=pop1();
            x2=pop2();
            x3=pop3();    
            return;
     }
     else{
            if(n>2){    
                *(t3+n-1)=*(t2+n-1);
                *(t2+n-1)='0';
                prnt();
                direc=1;   
          num--;                                           
                return;                
            }
            *(t3+1)=*(t2+1);
            *(t2+1)='0';
            prnt();         
            t3[0]=t1[0];        
            t1[0]='0';
            prnt();
            direc=-1;
            num=popn();                
            x1=pop1();
            x2=pop2();
            x3=pop3();  
            return;
     } 
    }
    /*  主函数,功能:辅初值,循环调用digui()函数,调用prnt()函数  */ 
    main(){
        int i;
        int j=0;
        printf("/*        HANOI塔问题的非递归解         */\n");
        printf("/* hanoinorecursive.c                   */\n\n");
        prnt();
        s1=s2=s3=(struct ptnode *)malloc((int)sizeof(struct ptnode));
        s1->ch=s2->ch=s3->ch=NULL;
        s1->next=s2->next=s3->next=NULL;
        nn=(struct numnode *)malloc((int)sizeof(struct numnode));
        nn->next=NULL; 
        direc=1;
        while(1){
            digui(num,x1,x2,x3);
            j++;
            /*if(s1->next==NULL&&direc>0)break;*/
            i=0;
            while(i<N&&c[i]!='0')i++;
            if(i==N)break;   
        } 
        /*printf("%3d",j);*/
        printf("\n");    
        getchar(); 
    }
    /* 屏幕打印函数 */
    void prnt(){
       int i;
       printf("\n");
       printf("STEP%3d\n",step++);
       printf("a:");
       for(i=0;i<N;i++)
            printf("%3c",a[i]);
        printf("\n");
       printf("b:");        
        for(i=0;i<N;i++)
            printf("%3c",b[i]);
        printf("\n");
        printf("c:");                                        
        for(i=0;i<N;i++)
            printf("%3c",c[i]);
        printf("\n\n");        
    }
      

  2.   

    // Han.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>int count;
    int autoPlay;
    int ifCls;void MoveOne(int base1, int base2, int **a)
    {
    a[base2][a[base2][count]] = a[base1][a[base1][count]-1];
    a[base1][a[base1][count]-1] = 0;
    a[base1][count] = a[base1][count] - 1;
    a[base2][count] = a[base2][count] + 1;
    }void sleep( clock_t wait )
    {
       clock_t goal;
       goal = wait + clock();
       while( goal > clock() )
          ;
    }void OutputState( int **a, int steps )
    {
    int i, j; if(ifCls == 0) system("cls");
    printf("\n\nStep: %5d\n\n", steps);
    for(i = count - 1; i >= 0; i--)
    {
    printf("\t||");
    for(j = 0; j < count; j++)
    {
    if(j < a[0][i]) 
    printf("O");
    else
    printf("_");
    }
    printf("||\t||");
    for(j = 0; j < count; j++)
    {
    if(j < a[1][i]) 
    printf("O");
    else
    printf("_");
    }
    printf("||\t||");
    for(j = 0; j < count; j++)
    {
    if(j < a[2][i]) 
    printf("O");
    else
    printf("_");
    }
    printf("||\n");
    }
    for(i = 0; i < 3*(count + 8) + 12; i++)
    printf("=");
    printf("\n\n");
    if(autoPlay == 0)
    getchar();
    else
    sleep( (clock_t)autoPlay * CLOCKS_PER_SEC/CLOCKS_PER_SEC );
    }int Han(int n, int b1, int b2, int b3, int **a)
    {
    static int steps = 0; if(n == 1)
    {
    MoveOne(b1, b3, a);
    steps++;
    OutputState( a, steps );
    return steps;
    }
    Han(n-1, b1, b3, b2, a);
    Han(1, b1, b2, b3, a);
    Han(n-1, b2, b1, b3, a); return steps;
    }int main(int argc, char* argv[])
    {
    int **a;
    int b1, b2, b3;
    int i, j; count = 5;
    autoPlay = 0;
    ifCls = 0;
    if(argc >= 2) count = atoi( argv[1] );
    if(argc >= 3) autoPlay = atoi( argv[2] );
    if(argc == 4) ifCls = 1;

    a = (int **)malloc(3 * sizeof(int *));
    for(i=0; i<3; i++)
    a[i] = (int *)malloc((count+1)*sizeof(int)); b1 = 0;
    b2 = 1;
    b3 = 2; for(i = 0; i < 3; i++)
    for(j = 0; j < count+1; j++)
    a[i][j] = 0;

    for(j = 0; j < count; j++)
    a[b1][j] = count - j;
    a[b1][count] = count;
    a[b2][count] = 0;
    a[b3][count] = 0; //printf("Step: %5d\n", 0);
    OutputState( a, 0 );
    int steps = Han(count, b1, b2, b3, a);
    printf("Total steps: %6d\n\n", steps); return 0;
    }
      

  3.   

    #include <stdio.h>
    void move(int n,int a,int b,int c)
    {if(n==1)printf("%d->%d\n",a,c);
     else
     {move(n-1,a,c,b);
      printf("%d->%d\n",a,c);
      move(n-1,b,a,c);
     }
    }
    main()
    {int n;
     scanf("%d",&n);
     move(n,1,2,3);
     printf("\n");
    }
      

  4.   

    //3.42  汉诺塔问题
    /*
    #include <iostream.h>void hanoi( int n, int pole1, int pole2, int pole3 ); 
    //n是盘子的数目,pole1是开始的柱子,pole3是作为目的地的柱子int main()
    {
    cout <<"移动顺序是:" <<endl;
    hanoi( 5, 1, 2, 3 );
    return 0;
    }void hanoi( int n, int pole1, int pole2, int pole3 )
    {
    if( n != 1 ) {
    hanoi( n - 1, pole1, pole3, pole2 );
    cout <<pole1 <<" --> " <<pole3 <<endl;
    hanoi( n -1, pole2, pole1, pole3 );
    }
    else
    cout <<pole1 <<" --> " <<pole3 <<endl;
    }  */