汉诺塔的非递归演示动画.docx
《汉诺塔的非递归演示动画.docx》由会员分享,可在线阅读,更多相关《汉诺塔的非递归演示动画.docx(7页珍藏版)》请在冰豆网上搜索。
汉诺塔的非递归演示动画
汉诺塔的非递归(演示、动画)
Hanoi,非递归,演示,动画效果
kensta
有动画演示,move2()是标准解的调用
move()是用于演示动画或显示移动秩序和包含监测有无错误移动的调用
使用Borlandc++3.0(Turboc++3.0,Turboc2.0也可)编译通过,图形方式使用Tc的bgi
*/
/*************************************/
/*
abouterrorprocess
*/
#include
#include
#include
/*
ifdebuginguse#defineERROR_DEBUG
otherwiseremoveit.
*/
//#defineERROR_DEBUG
#ifdefERROR_DEBUG
#defineerror(x)error_debug(x)
#definereport()report_debug()
#defineiniterror()initerror_debug()
char*err[10];
interrs=0;
voidiniterror_debug(){
inti;
for(i=0;i<10;i++)err[i]=NULL;
}
voiderror_debug(char*a){
if(errs>9)return;
err[errs]=(char*)malloc(strlen(a)+1);
strcpy(err[errs],a);
printf(a);
errs++;
}
voidreport_debug(){
inti;
if(!
errs)return;
for(i=0;iprintf(err[i]);
free(err[i]);
}
}
#else
#defineerror(x)
#definereport()
#defineiniterror()
#endif
/*************************************/
/*
aboutstack
*/
#defineSTACK_SIZE31
typedefstruct{
intdata[STACK_SIZE];
inttop;
}stack;
intclear(stack*a);
intcreate(stack**a);
intpush(stack*a,intdata);
intpop(stack*a,int*data);
intgettop(stack*a,int*data);
intdispose(stack*a);
intpop(stack*a,int*data){
if(a->top){
*data=a->data[--a->top];
return1;
}else{
error("pop(stack*,int*):
stackempty!
\n");
return0;
}
}
intpush(stack*a,intdata){
if(a->topa->data[a->top++]=data;
return1;
}else{
error("push(stack*,int):
stackfull!
\n");
return0;
}
}
intcreate(stack**a){
*a=(stack*)malloc(sizeof(stack));
if(*a)returnclear(*a);
else{
error("create(stack**):
createerror!
Notenoughmomery!
\n");
return0;
}
}
intclear(stack*a){
if(a){
a->top=0;
return1;
}else{
error("clear(stack*):
stacknotexist!
\n");
return0;
}
}
intgettop(stack*a,int*data){
if(a->top){
*data=a->data[a->top-1];
return1;
}else{
error("gettop(stack*,int*):
stackempty!
\n");
return0;
}
}
intdispose(stack*a){
if(a){
free(a);
return1;
}else{
error("dispose(stack*):
stacknotexist!
\n");
return0;
}
}
/**************************************/
/*
aboutHanoithegame
*/
#include
#include
#defineMAX_LEVELSTACK_SIZE
intposition[MAX_LEVEL+1];
stack*theStack[3];
intdepth;
intmode;
intprint;
intinitgame(intd){
inti;
intx,y;
inth=5;
intw;
initerror();
if(mode){
intgdriver=DETECT,gmode,errorcode;
/*initializegraphicsmode*/
initgraph(&gdriver,&gmode,"");
setfillstyle(1,7);
}
for(i=0;i<3;i++)
if(!
create(&theStack[i]))
break;
if(i!
=3){
for(;i>=0;i--)dispose(theStack[i]);
error("initgame(int):
cannotinitstack!
\n");
return0;
}
depth=d;
for(i=d;i;i--){
push(theStack[0],i);
if(mode){
y=200+100-theStack[0]->top*(h+1);
w=i*10;
x=150-w/2;
setcolor(i);
setfillstyle(1,i);
bar(x,y,x+w,y+h);
}
position[i]=0;
}
if(mode){
setcolor(15);
for(i=0;i<3;i++)
rectangle(150+i*150-1,120,150+i*150+1,300);
line(50,300,500,300);
}
return1;
}
intendgame(){
inti=2;
for(;i>=0;i--)dispose(theStack[i]);
printf("report:
");
report();
if(mode)closegraph();
return1;
}
voidshow(intp,intfrom,intto){
inti;
intx,y;
intnewx,newy;
inth=5;
intw=p*10;
y=200+100-(theStack[from]->top+1)*(h+1);
x=from*150+150-w/2;
newx=to*150+150-w/2;
newy=200+100-theStack[to]->top*(h+1);
while(y>100){
setcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
y-=(h+1);
setcolor(15);
rectangle(150+from*150-1,120,150+from*150+1,300);
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay(10);
}
while(x!
=newx){
setcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
(x>newx)?
x--:
x++;
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay
(2);
}
while(ysetcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
setcolor(15);
rectangle(150+to*150-1,120,150+to*150+1,300);
y+=(h+1);
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay(10);
}
}
intmove(intp){
intt,s;
if(!
gettop(theStack[position[p>,&t)){
error("move(int):
thestackisempty\n");
return0;
}
if(t==p){
pop(theStack[position[p>,&t);
if(!
mode&&print)printf("%c->",'A'+position[p]);
/*anotherimportantcoreline*/
s=(position[p]+1+(depth%2?
p%2:
(p+1)%2))%3;
if(gettop(theStack[s],&t)&&t
error("move(int):
cannotmovebiglevelabovesmallone\n");
return0;
}
push(theStack[s],p);
if(mode)show(p,position[p],s);
elseif(print)printf("%c\t",'A'+s);
position[p]=s;
}elseerror("move(int):
positionerror\n");
return1;
}
intmove2(intp){
intt,s;
s=(position[p]+1+(depth%2?
p%2:
(p+1)%2))%3;
if(print)printf("%c->%c\t",'A'+position[p],'A'+s);
position[p]=s;
return1;
}
#include
voidmain(){
unsignedlongi;
unsignedlongN=10;
unsignedlongp,q;
printf("WelcometoHanoi\n");
printf("NotethatthisHanoiisnotwritebyrecurrence!
\n");
printf("Andnotcalculatewithanystack.\n");
printf("butiwanttocheckiftheaisright.\n");
printf("iuse3stacktoshowifthereisanyviolentmovehappens.:
)\n");
printf("\nEnteranumberaslevel(1to30):
");
scanf("%d",&N);
if(N<1||N>30){
printf("error:
not1to30\n");
return;
}
printf("\nSelectshowmode('c'inTEXT'g'inGRAPHICS)\n");
printf("Notethatifthelevelistobigyou'dbetternotuse'g'forspeed.\n");
printf("19isabout20seconds.20isaboutdoubleofthat.etc.\n");
printf("Itestonaintel166mmxcpu.30maybe40*1024seconds.\n");
printf("wishyousucceed!
\n");
switch(getch()){
case'c':
printf("doyouwanttoshowtheresult?
(y/n)\n");
printf("printresultwillbeslow!
!
!
\n");
do{
mode=getch();
if(mode=='y')print=1;
if(mode=='n')print=0;
}while(mode!
='y'&&mode!
='n');
mode=0;
break;
case'g':
mode=1;break;
default:
printf("error:
neither'c'nor'g'\n");return;
}
printf("processing...\n");
initgame(N);
/*
corehere!
!
!
only8lines,ha!
heregetthelevelqueue
as121312141213121
*/
for(i=1;i<(1L<q=1L<while(q&&i%q){
q>>=1;
p--;
}
if(mode||print)move(p);
elsemove2(p);
}
printf("ok\n");
getch();
endgame();
}