首页 > 编程知识 正文

汉诺塔为什么叫汉诺塔,汉诺塔比赛用什么型号的汉诺塔

时间:2023-05-04 10:37:49 阅读:194874 作者:4239

#includeint num1,num2,num3,h0,cy[66];

void plot1(int ,int ,int ,int );

void move(char getone,char putone)

{ int x0=40,x,y,w,h,tx,ty,tw,th,x1,x2,y1,y2,xm1,ym1,xm2,ym2;

int i,n,size;

void *buffer,*buffer1;

switch(getone)

{

case 'A':num1--;break;

case 'B':num2--;break;

case 'C':num3--;break;

default: exit(0);

}

switch(putone)

{

case 'A':num1++;break;

case 'B':num2++;break;

case 'C':num3++;break;

default: exit(0);

}

switch(getone)

{

case 'A':x=120;y1=cy[num1+1]-h0;break;

case 'B':x=320;y1=cy[num2+1]-h0;break;

case 'C':x=520;y1=cy[num3+1]-h0;break;

default: exit(0);

}

switch(putone)

{

case 'A':tx=120-50;ty=cy[num1]-h0;break;

case 'B':tx=320-50;ty=cy[num2]-h0;break;

case 'C':tx=520-50;ty=cy[num3]-h0;break;

default: exit(0);

}

x2=x+50;

y2=y1+h0;

xm1=(x1+tx)/2;

ym1=(y1+ty)/2;

xm2=xm1+100;

ym2=ym1+h0;

size=imagesize(x1,y1,x2,y2);

getimage(x1,y1,x2,y2,buffer);

setfillstyle(SOLID_FILL,3);

bar(x1,y1,x2,y2);

setcolor(YELLOW);

line((x1+x2)/2,y1,(x1+x2)/2,y2);

n=abs(tx-x1);

for(i=0;i{

xm1=x1+(float)(tx-x1)/n*i;

ym1=y1+(float)(ty-y1)/(tx-x1)*(xm1-x1);

xm2=xm1+100;

ym2=ym1+h0;

getimage(xm1,ym1,xm2,ym2,buffer1);

putimage(xm1,ym1,buffer,COPY_PUT);/**//* delay(10); */

putimage(xm1,ym1,buffer1,COPY_PUT);

}

putimage(tx,ty,buffer,COPY_PUT);/**//* delay(50);*/

free(buffer1);

free(buffer);

}

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

}

}

main()

{

int gdriver,gmode,x0=40,y0=400,x1,x2,x3,y1,y2,y3,x,y,w0=100;

int i,num,w,w1;

printf("Please input the number of plate(<=64):");

scanf("%d",&num);

if(num>65)

{printf ("number great than 64 ",exit(0));}

h0=(y0-80)/num;

num1=num;

num2=0;

num3=0;

gdriver=DETECT;

initgraph(&gdriver,&gmode,"");

setfillstyle(SOLID_FILL,3);

bar(0,0,640,480);

setcolor(15);

line(x0+80,40,x0+80,400);

line(x0+280,40,x0+280,400);

line(x0+480,40,x0+480,400);

x2=x0+280;

x3=x0+480;

for(x=x1,y=y0,w=w0,i=1;i<=num;i++,y=y-h0-1,w=w-w1)

{

plot1(x,y,w,h0);

cy[i]=y;

}

setcolor(14);

line(x0+80,40,x0+80,400);

getch();

hanoi(num,'A','B','C');

getch();

}

void plot1(int x,int y,int w,int h)

{int x1,x2,y1,y2,xc,yc,a,b;

x2=x+w/2;

y1=y-h;

y2=y;

xc=(x1+x2)/2;

yc=(y1+y2)/2;

a=w/2;

b=h/2;

setfillstyle(SOLID_FILL,14);

bar(x1,y1,x2,y2);

setlinestyle(0,0,1);

line(x1,y1,x1,y2);

line(x1,y2,x2,y2);

line(x2,y2,x2,y1);

line(x2,y1,x1,y1);

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。