汉诺塔的C语言代码怎么写啊
发布网友
发布时间:2022-04-22 05:54
我来回答
共3个回答
热心网友
时间:2023-09-29 14:01
/*5. 源程序*/
/********hanoi.c*********/
#include <graphics.h>
struct H
{
int data[15];/*存放每个盘的代号*/
int top;/*每个塔的具体高度*/
}num[3];/*三个塔*/
void move(char x,char y,struct H num[3]);/*移动的具体过程*/
void hanoi(char x,char y,char z,int n,struct H num[3]);/*递归*/
void Init(void);/*初始化*/
void Close(void);/*图形关闭*/
int computer=1;/*自动控制与手动控制的标志*/
int speed=0;/*全局变量speed主要是演示过程的速度*/
void main(void)
{
Init();/*初始状态*/
Close();/*图形关闭*/
exit(0);
}
void Init(void)/*初始化*/
{
int gd=DETECT,gm;
int i,n,color;
clrscr();
printf("please input n(n<=10): ");/*输入要演示的盘子数*/
scanf("%d",&n);
printf("Please input 1 or 2:\n1.computer 2.people\n");
scanf("%d",&i);
if(i==2)/*选择手动控制标志为0*/
computer=0;
if(n<1||n>10)
n=10;/*越界的话n当10处理*/
if(computer)/*如果是自动控制的话输入速度*/
{
printf("please input speed: ");/*输入速度*/
scanf("%d",&speed);
}
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
for(i=0;i<3;i++)
num[i].top=-1;/*三个地方的高度开始都为-1*/
for(i=0;i<n;i++)/*画一开始的塔座A上的盘子*/
{
num[0].top++;/*栈的高度加1*/
num[0].data[num[0].top]=i; /*最大的盘子代号为0,依次为1,2,…n-1*/
color=num[0].data[num[0].top]+1;/*盘子的颜色代码为栈顶盘子代号加1*/
setfillstyle(SOLID_FILL,color);
bar(100-(33-3*num[0].data[num[0].top]),400-20*i-8,100+
(33-3*num[0].data[num[0].top]),400-20*i+8); /*画矩形*/
}
setcolor(YELLOW);
outtextxy(180,450,"any key to continue");
settextstyle(0,0,2);
outtextxy(90,420,"A"); /*塔座标志*/
outtextxy(240,420,"B");
outtextxy(390,420,"C");
getch();/*接收字符后就执行递归操作*/
hanoi('a','b','c',n,num);
}
void move(char x,char y,struct H num[3])/*移动的具体过程*/
{
int i;
char num1[3],num2[3];
sprintf(num1,"%c",x-32);/*将小写变成大写,并转换成字符串输出*/
sprintf(num2,"%c",y-32);
setfillstyle(SOLID_FILL,BLACK);/*把原来的地方移去涂黑*/
bar(0,0,640,60);
setcolor(RED);
outtextxy(150,30,num1);/*输出移动过程*/
outtextxy(200,30,"--->");
outtextxy(310,30,num2);
settextstyle(0,0,2);
setfillstyle(SOLID_FILL,BLACK);/*把原来的地方移去涂黑*/
bar(100+150*(x-97)-(33-3*num[x-97].data[num[x-97].top]),
400-20*num[x-97].top-8,100+150*(x-97)+(33-3*
num[x-97].data[num[x-97].top]),400-20*num[x-97].top+8);
num[y-97].top++;/*入栈,目标点的top加1*/
num[y-97].data[num[y-97].top]=num[x-97].data[num[x-97].top];/*在目标点盘子的代号与源点盘子的代号相同*/
num[x-97].top--;/*出栈,原来地方的top减1*/
setfillstyle(SOLID_FILL,num[y-97].data[num[y-97].top]+1);/*盘子颜色代码是栈顶盘子代号加1*/
bar(100+150*(y-97)-(33-3*num[y-97].data[num[y-97].top]),
400-20*num[y-97].top-8,100+150*(y-97)+
(33-3*num[y-97].data[num[y-97].top]),400-20*num[y-97].top+8);
if(computer)/*自动控制就用delay*/
delay(speed);/*延时函数*/
else
getch();/*手动控制的话就自己按键盘来控制*/
}
void hanoi(char one,char two,char three,int n,struct H num[3])/*递归n为盘子数,num为堆栈*/
{
if(n==1)
move(one,three,num);/*如果盘子为1,将这个盘子从塔座A移动到塔座C*/
else
{
hanoi(one,three,two,n-1,num);/*将塔座A的前n-1个盘子移到塔座B*/
move(one,three,num);/*将塔座A的第n个盘子移到塔座C*/
hanoi(two,one,three,n-1,num); /*将塔座B的n-1个盘子移到塔座C*/
}
}
void Close(void)/*图形关闭*/
{
getch();
closegraph();
}
热心网友
时间:2023-09-29 14:01
#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);
*/
}
热心网友
时间:2023-09-29 14:01
#include
void
solve(int
num,int
z1,int
z2,int
z3);
void
main()
{
int
n;
printf("请输入盘子数量:");
scanf("%d",&n);
solve(n,1,2,3);
}
void
solve(int
num,int
z1,int
z2,int
z3)
{
if(num>0){
solve(num-1,z1,z3,z2);
printf("把%d上的盘子移到%d上\n",z1,z2);
solve(num-1,z3,z2,z1);
}
}