汉诺塔的非递归演示动画.docx

上传人:b****2 文档编号:1234200 上传时间:2022-10-19 格式:DOCX 页数:7 大小:16.55KB
下载 相关 举报
汉诺塔的非递归演示动画.docx_第1页
第1页 / 共7页
汉诺塔的非递归演示动画.docx_第2页
第2页 / 共7页
汉诺塔的非递归演示动画.docx_第3页
第3页 / 共7页
汉诺塔的非递归演示动画.docx_第4页
第4页 / 共7页
汉诺塔的非递归演示动画.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

汉诺塔的非递归演示动画.docx

《汉诺塔的非递归演示动画.docx》由会员分享,可在线阅读,更多相关《汉诺塔的非递归演示动画.docx(7页珍藏版)》请在冰豆网上搜索。

汉诺塔的非递归演示动画.docx

汉诺塔的非递归演示动画

汉诺塔的非递归(演示、动画)

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;i

printf(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->top

a->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(y

setcolor(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;

p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 互联网

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1