栈与队列!也许不简单......

时间一粒 2010-05-27 01:08:45
题目:4.有A,B,C三个塔座,A上套有n个直径不同的圆盘,按直径从大到小叠放,形如宝塔,
编号1,2,3……n。要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循下列原则:

a.每次只能移一个圆盘

b.圆盘可在三个塔座上任意移动

c.任何时刻,每个塔座上不能将大盘压到小盘上

望手给个算法或者代码!
鄙人不胜感激!
...全文
202 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
augustinlouis 2010-05-31
  • 打赏
  • 举报
回复
n年以前的代码。。。。

#include <stdio.h>
#define N 10
typedef struct hanoi{
int n;
char a ; char b ; char c ;
} hanoi ;

hanoi p[N+1];
int top = 1;

int count = 0;

void push( hanoi p[] , int n, char a, char b, char c){
p[top].n = n;
p[top].a = a;
p[top].b = b;
p[top].c = c;
top ++ ;
}

void not_recur_hanoi(int n){ // n = [1,N] n is the number of disk
char a = 'A' , b = 'B' , c = 'C' ;
push( p , n , a , b , c);
for( ; top > 1 ; ){ // not void
for( ; p[top-1].n > 1 ; ) {
push( p , p[top-1].n - 1 , p[top-1].a , p[top-1].c , p[top-1].b) ;
}

printf( " move disk %d from %c to %c \n",
p[top-1].n , p[top-1].a , p[top-1].c ); // 顶为1,底为n
count ++ ;
top -- ;

if( top > 1){
printf( " move disk %d from %c to %c \n",
p[top-1].n , p[top-1].a , p[top-1].c );
count ++ ;
char temp_a = p[top-1].a , temp_b = p[top-1].b ,temp_c = p[top-1].c,
temp_n = p[top-1].n;
top -- ;
push ( p, temp_n - 1 , temp_b , temp_a , temp_c);
}
}
}

int main(){
not_recur_hanoi( 4 ); // must be less than N

printf("the number of movement is %d " , count) ;


return 0;
}


时间一粒 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yq_118 的回复:]
递归最终还是靠栈实现的。
[/Quote]
对啊,就是要用栈来实现啊!
所以才来请教高手...
stein42 2010-05-27
  • 打赏
  • 举报
回复
递归最终还是靠栈实现的。
augustinlouis 2010-05-27
  • 打赏
  • 举报
回复
非递归也不难。。。
东莞某某某 2010-05-27
  • 打赏
  • 举报
回复
我以为用非递归实现。。。
vanchristin 2010-05-27
  • 打赏
  • 举报
回复
汉诺塔吧
谭浩强那本书上就有例子
yuanzhang198711 2010-05-27
  • 打赏
  • 举报
回复
都是经典的算法啦,应该提出些创新性的问题和见解,这样才能带动话题嘛。
周靖峰 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhzxlc_06 的回复:]
C/C++ code
#include <stdio.h>

void hanoi(int n,char one,char two,char three);
void move(char x,char y);

int main(void)
{
int n;
printf("input the number of diskes:");
scanf("%d……
[/Quote]
时间一粒 2010-05-27
  • 打赏
  • 举报
回复
呵呵~谢谢大家的热情回答!
chaoliu1024 2010-05-27
  • 打赏
  • 举报
回复
#include <stdio.h>

void hanoi(int n,char one,char two,char three);
void move(char x,char y);

int main(void)
{
int n;
printf("input the number of diskes:");
scanf("%d",&n);
printf("The step to moving %d diskes:\n",n);
hanoi(n,'A','B','C');

return 0;
}

void hanoi(int n,char one,char two,char three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}

void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
liao05050075 2010-05-27
  • 打赏
  • 举报
回复
这个不就是汉诺塔问题吗。。
最经典的递归算法。
自己搜索一下吧,代码,讲解有的是
chaoliu1024 2010-05-27
  • 打赏
  • 举报
回复
这个是Hanoi(汉诺)塔问题!网上有很多的!
lylm 2010-05-27
  • 打赏
  • 举报
回复
错了,google 汉诺塔
ysd19850101 2010-05-27
  • 打赏
  • 举报
回复
c语言教程里有这个标准程序
lylm 2010-05-27
  • 打赏
  • 举报
回复
google 巴别塔
stein42 2010-05-27
  • 打赏
  • 举报
回复
汉诺塔问题,自己去搜吧。

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧