回 帖 发 新 帖 刷新版面

主题:[原创]汉诺塔问题

汉诺塔是一个递归调用的典型例子。由于问题本身具有递归的特性,所以用递归很简单。代码如下:(程序绝对正确)
#include <stdio.h>
void hanoi(int n,char x,char y,char z);
void move(char x,int n,char y);
void main()
{
    int num;
    printf("Please enter the number of disks num:\n");
    scanf("%d",&num);
    hanoi(num,'a','b','c');
}
void hanoi(int n,char x,char y,char z)
{
    if(n==1)
        move(x,1,z);
    else{
        hanoi(n-1,x,z,y);
        move(x,n,z);
        hanoi(n-1,y,x,z);
    }
}
void move(char x,int n,char y)
{
    printf("%c->%d->%c\n",x,n,y);
}

回复列表 (共10个回复)

沙发

楼主,你的意思是?

板凳

偶也不懂,不太清楚!

3 楼

这是学习递归算法比较经典的例子

4 楼

[em18]真的是好难懂啊

5 楼

这是一个很有趣的例子,想通了,就更觉得有趣,用来说明递归,是相当恰当的。基本的思想就是:知道了N层汉诺塔的移法,那么N+1层也就知道怎么办了。我们肯定是知道两层的移法的。所以,依次就可以推得N层的移法了。

6 楼

不知道楼主是什么意思?
   想告诉我们什么哪?
学过编程语言的人都知道汉诺塔的递归调用是最经典的,但是能看懂的人还为数不多

7 楼

大家能用c++编写一下该程序吗?谢谢!

8 楼

看我的用字符图做的,有动画声音呀。
///////////////////////////////////////////////////////////////////////
/// HANOI.CPP
typedef int ElemType;
#include <stdio.h>
#include <dos.h>
#include <malloc.h>
#include <wgku.h>
#include "stack1.h"
#include <conio.h>
void pause(int);
void printt(int,int,int,int);
void move(char,char);
void printmap(void);
void hanoi(int,char,char,char);
void printh(void);
SqStack T1,T2,T3;
int n;
void main()

{int ii;
InitStack(T1);
InitStack(T2);
InitStack(T3);
textbackground(1);
textcolor(4);
clrscr();
do
{ gotoxy(10,24);printf("Input the disk of diskes:");
scanf("%d",&n);}while(n>13);
for(ii=1;ii<=n;ii++)
   Push(T1,(n-ii)*2+3);
// StackTraverse(T1,print);
printmap();
hanoi(n,'A','B','C');
DestroyStack(T1);
DestroyStack(T2);
DestroyStack(T3);
getch();
textbackground(8);
textcolor(7);
clrscr();
}
void move(char a,char b)
{
  int i,j,k,l,m,n;
  gotoxy(30,22);cprintf("\n%c-->%c",a,b);
  switch(a)
  {
   case 'A':
    i=1;
    m=StackLength(T1);
    Pop(T1,k);
    break;
   case 'B':
    i=2;
    m=StackLength(T2);
    Pop(T2,k);
    break;
   case 'C':
    i=3;
    m=StackLength(T3);
    Pop(T3,k);
    break;
   }
  switch(b)
  {
   case 'A':
    j=1;
    n=StackLength(T1);
    Push(T1,k);
    break;
   case 'B':
    j=2;
    n=StackLength(T2);
    Push(T2,k);
    break;
   case 'C':
    j=3;
    n=StackLength(T3);
    Push(T3,k);
    break;
   }
for(l=20-m+1;l>=1;l--)
{
//  printh();
  textcolor(l/2+2);
  printt(13+(i-1)*26-k/2,l,k,1);
  sound(440+l*40);
  pause(2+l);
  nosound();
  printt(13+(i-1)*26-k/2,l,k,0);
  printh();
}
for(l=1;l<20-n+1;l++)
{
//  printh();
  textcolor(l/2+2);
  if(l-1>=1)
  printt(13+(j-1)*26-k/2,l-1,k,0);

  printt(13+(j-1)*26-k/2,l,k,1);
  sound(2800+l*40);
  pause(22-l);
  nosound();
  printh();
}
}

void hanoi(int n,char a,char b,char c)
{
if(n==1)
    move(a,c);
else
{
    hanoi(n-1,a,c,b);
    move(a,c);
    hanoi(n-1,b,a,c);
}
}
void printmap(void)
{
int i,j;
gotoxy(2,21);
textcolor(15);
for(i=2;i<=79;i++)
   cprintf("%c",220);
for(i=1;i<=n;i++)
{
   textcolor(13-i);
   printt(13-(n-i+1),20-i+1,(n-i)*2+3,1);
   //printt(13-(n-i+1),20-i+1,(n-i)*2+3,0);
  }
  textcolor(12);
printh();
gotoxy(13,22);cprintf("%c",'A');
gotoxy(39,22);cprintf("%c",'B');
gotoxy(65,22);cprintf("%c",'C');
}
void printt(int x,int y,int m,int i)
{
  int j=m;
  gotoxy(x,y);
  while(--j>=0)
    if(i==1) cprintf("%c",220);else cprintf("%c",' ');
}
void pause(int i)
{
int j,k;
for(j=0;j<=100*i;j++)
for(k=0;k<=10500;k++);
}
void printh(void)
{int i;
// textcolor(6);
for(i=8;i<=20;i++)
{
  gotoxy(13,i);cprintf("%c",177);
  gotoxy(39,i);cprintf("%c",177);
  gotoxy(65,i);cprintf("%c",177);
}
}


////////////////////////////////////////////////////////////////////
//stack.h
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;

status InitStack(SqStack &S)
{
    S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if(!S.base) return ERROR;
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
status DestroyStack(SqStack &S)
{
    free(S.base);
    S.base=S.top=NULL;
    S.stacksize=0;
    return OK;
}
status CrearStack(SqStack &S)
{
    if(S.base==NULL || S.stacksize==0) return ERROR;
    S.top=S.base;
    return OK;
}
status StackEmpty(SqStack &S)
{
    if(S.base==S.top) return TRUE;
    else return FALSE;
}
status GetTop(SqStack S,ElemType &e)
{
    if(S.base==S.top) return ERROR;
    e=*(S.top-1);
    return e;
}
status Push(SqStack &S,ElemType e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
        if(!S.base) return ERROR;
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}
status Pop(SqStack &S,ElemType &e)
{
    if(S.base==S.top) return ERROR;
    e=*--S.top;
    return OK;
}
status StackTraverse(SqStack S,status(* stack)(ElemType))
{
    ElemType *p;
    if(S.base==S.top) return ERROR;
    for(p=S.top;p>S.base;p--)
    {
        if(!stack(*(p-1))) return ERROR;
    }
    return OK;
}
status print(ElemType e)
{
    printf(" %d",e);
    return OK;
}
status StackLength(SqStack S)
{
    return S.top-S.base;
}

9 楼

... ...

10 楼

有动画界面的,所以复杂点。。。呵,[em1][em1][em1]

我来回复

您尚未登录,请登录后再回复。点此登录或注册