c语言 模拟汉诺塔游戏的解法 模拟汉诺塔解法的C程序

\u5982\u4f55\u505a\u4e00\u4e2aC\u8bed\u8a00\u7f16\u7a0b\u7684\u6c49\u8bfa\u5854\u6e38\u620f\uff1f\u8981\u6709\u6e90\u4ee3\u7801\u3002

#include
void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
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 m;
printf("input the number of disks:");
scanf("%d",&m);
printf("the step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
}
\u7b97\u6cd5\u4ecb\u7ecd\uff1a
\u5176\u5b9e\u7b97\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5f53\u76d8\u5b50\u7684\u4e2a\u6570\u4e3an\u65f6\uff0c\u79fb\u52a8\u7684\u6b21\u6570\u5e94\u7b49\u4e8e2^n \u2013 1\uff08\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u5df1\u8bc1\u660e\u8bd5\u8bd5\u770b\uff09\u3002\u540e\u6765\u4e00\u4f4d\u7f8e\u56fd\u5b66\u8005\u53d1\u73b0\u4e00\u79cd\u51fa\u4eba\u610f\u6599\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u53ea\u8981\u8f6e\u6d41\u8fdb\u884c\u4e24\u6b65\u64cd\u4f5c\u5c31\u53ef\u4ee5\u4e86\u3002\u9996\u5148\u628a\u4e09\u6839\u67f1\u5b50\u6309\u987a\u5e8f\u6392\u6210\u54c1\u5b57\u578b\uff0c\u628a\u6240\u6709\u7684\u5706\u76d8\u6309\u4ece\u5927\u5230\u5c0f\u7684\u987a\u5e8f\u653e\u5728\u67f1\u5b50A\u4e0a\uff0c\u6839\u636e\u5706\u76d8\u7684\u6570\u91cf\u786e\u5b9a\u67f1\u5b50\u7684\u6392\u653e\u987a\u5e8f\uff1a\u82e5n\u4e3a\u5076\u6570\uff0c\u6309\u987a\u65f6\u9488\u65b9\u5411\u4f9d\u6b21\u6446\u653e A B C\uff1b
\u82e5n\u4e3a\u5947\u6570\uff0c\u6309\u987a\u65f6\u9488\u65b9\u5411\u4f9d\u6b21\u6446\u653e A C B\u3002
\uff081\uff09\u6309\u987a\u65f6\u9488\u65b9\u5411\u628a\u5706\u76d81\u4ece\u73b0\u5728\u7684\u67f1\u5b50\u79fb\u52a8\u5230\u4e0b\u4e00\u6839\u67f1\u5b50\uff0c\u5373\u5f53n\u4e3a\u5076\u6570\u65f6\uff0c\u82e5\u5706\u76d81\u5728\u67f1\u5b50A\uff0c\u5219\u628a\u5b83\u79fb\u52a8\u5230B\uff1b\u82e5\u5706\u76d81\u5728\u67f1\u5b50B\uff0c\u5219\u628a\u5b83\u79fb\u52a8\u5230C\uff1b\u82e5\u5706\u76d81\u5728\u67f1\u5b50C\uff0c\u5219\u628a\u5b83\u79fb\u52a8\u5230A\u3002
\uff082\uff09\u63a5\u7740\uff0c\u628a\u53e6\u5916\u4e24\u6839\u67f1\u5b50\u4e0a\u53ef\u4ee5\u79fb\u52a8\u7684\u5706\u76d8\u79fb\u52a8\u5230\u65b0\u7684\u67f1\u5b50\u4e0a\u3002\u5373\u628a\u975e\u7a7a\u67f1\u5b50\u4e0a\u7684\u5706\u76d8\u79fb\u52a8\u5230\u7a7a\u67f1\u5b50\u4e0a\uff0c\u5f53\u4e24\u6839\u67f1\u5b50\u90fd\u975e\u7a7a\u65f6\uff0c\u79fb\u52a8\u8f83\u5c0f\u7684\u5706\u76d8\u3002\u8fd9\u4e00\u6b65\u6ca1\u6709\u660e\u786e\u89c4\u5b9a\u79fb\u52a8\u54ea\u4e2a\u5706\u76d8\uff0c\u4f60\u53ef\u80fd\u4ee5\u4e3a\u4f1a\u6709\u591a\u79cd\u53ef\u80fd\u6027\uff0c\u5176\u5b9e\u4e0d\u7136\uff0c\u53ef\u5b9e\u65bd\u7684\u884c\u52a8\u662f\u552f\u4e00\u7684\u3002
\uff083\uff09\u53cd\u590d\u8fdb\u884c\uff081\uff09\uff082\uff09\u64cd\u4f5c\uff0c\u6700\u540e\u5c31\u80fd\u6309\u89c4\u5b9a\u5b8c\u6210\u6c49\u8bfa\u5854\u7684\u79fb\u52a8\u3002
\u6240\u4ee5\u7ed3\u679c\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u6309\u7167\u79fb\u52a8\u89c4\u5219\u5411\u4e00\u4e2a\u65b9\u5411\u79fb\u52a8\u91d1\u7247\uff1a
\u59823\u9636\u6c49\u8bfa\u5854\u7684\u79fb\u52a8\uff1aA\u2192C,A\u2192B,C\u2192B,A\u2192C,B\u2192A,B\u2192C,A\u2192C
\u6c49\u8bfa\u5854\u95ee\u9898\u4e5f\u662f\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u7684\u7ecf\u5178\u9012\u5f52\u95ee\u9898\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u7ed9\u51fa\u9012\u5f52\u548c\u975e\u9012\u5f52\u7684\u4e0d\u540c\u5b9e\u73b0\u6e90\u4ee3\u7801\u3002

#include "graphics.h"
#include "dos.h"
#include "stdio.h"
#include "alloc.h"
int 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);
}
x1=x-50;
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);
buffer=malloc(size);
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);
buffer1=malloc(size);
n=abs(tx-x1);
for(i=0;i<n;i+=3)
{
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;
w1=(100-10)/num;
num1=num;
num2=0;
num3=0;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
setfillstyle(SOLID_FILL,3);
bar(0,0,640,480);
setcolor(15); /* white */
line(1,1,637,1);
line(1,1,1,477);
line(0,0,638,0);
line(0,0,0,478);
setcolor(8); /*darkgray */
line(638,2,638,478);
line(639,1,639,479);
line(632,380,632,472);
setcolor(14); /* yellow */
line(x0,401,640-x0,401);
line(x0+80,40,x0+80,400);
line(x0+280,40,x0+280,400);
line(x0+480,40,x0+480,400);
x1=x0+80;
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;
x1=x-w/2;
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); */
setcolor(14); /* yellow */
setlinestyle(0,0,1);
ellipse(xc,yc,0,360,a,b);
floodfill(xc-2,yc,YELLOW);
floodfill(xc+2,yc,YELLOW);
setcolor(1);
ellipse(xc,yc,0,360,a,b);
ellipse(xc,yc,0,360,a-3,b-3);
setfillstyle(SOLID_FILL,12);
floodfill(xc-2,yc+b-2,BLUE);
floodfill(xc+2,yc+b-3,BLUE);
/* line(x1,y1,x1,y2);
line(x1,y2,x2,y2);
line(x2,y2,x2,y1);
line(x2,y1,x1,y1);
*/
}


//\u5728TC\u4e0b\u7f16\u8bd1\u8fd0\u884c\uff0cVC\u7684\u8bdd\u4e0d\u884c\uff0c\u56e0\u4e3acfree\u91cc\u6ca1\u6709graphics.h

写一个函数用递归实现
用char代表盘子,ABC代表装盘子的平台
最终将A上所有盘子移到C上
void hanoi(int n, char A, char B, char C) {

if (n == 1) {

move(A, C); //只有一个盘子,把它从A移到C上
} else {
hanoi(n-1,A,C,B);//将A上n-1个盘子移到B上
move(A,C); //只有一个盘子,把A移到C上
hanoi(n-1,B,A,C);//将B上的盘子移到C上
}

}

你加点分呗,加点分我给你答案!呵呵~~

汉诺塔游戏是什么?没听过~~

扩展阅读:少儿编程小游戏 ... 汉诺塔5层31全图图解 ... 下载233乐园 ... 国家承认的少儿编程比赛 ... 如何自己编程做游戏 ... 汉诺塔图解一步一图 ... 汉诺塔十句口诀 ... 汉诺塔游戏手机版 ... 游戏破解器100%破解 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网