📄 hanio.c
字号:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#define MAXNUM 10
int hole[3][MAXNUM];
int plate_height[3]={0};
void drawplate(int x,int y,int wide)
{
setfillstyle(SOLID_FILL,wide/20);
bar(x-wide/2,y,x+wide/2,y+16-1);
}
void eraseplate(int x,int y,int wide)
{
setfillstyle(SOLID_FILL,BLACK);
bar(x-wide/2,y,x+wide/2,y+16-1);
}
void move(int x,int y)
{
int px,py,wide;
if(getch()==27)
{
closegraph();
exit(1);
}
wide=hole[x-1][plate_height[x-1]-1]*20;
px=100+200*(x-1);
py=480-plate_height[x-1]*16;
eraseplate(px,py,wide);
setfillstyle(SOLID_FILL,x-1+11);
bar(100+200*(x-1)-5,py,100+200*(x-1)+5,py+16-1);
hole[y-1][plate_height[y-1]]=hole[x-1][plate_height[x-1]-1];
plate_height[x-1]--;
plate_height[y-1]++;
px=100+200*(y-1);
py=480-plate_height[y-1]*16;
drawplate(px,py,wide);
delay(400);
}
void hanoi(int n,int one,int two,int three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void main()
{
int m,i;
int gdriver=DETECT,gmode;
printf("input the number of diskes:");
scanf("%d",&m);
if(m>10)
{
printf("Too many plates!\nYou would kill me?\n");
return;
}
for(i=m;i>=1;i--)
hole[0][m-i]=i;
plate_height[0]=m;
plate_height[1]=plate_height[2]=0;
printf("press any key to see the moving steps.");
getch();
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
for(i=0;i<3;i++)
{
setfillstyle(SOLID_FILL,11+i);
bar(100+200*i-5,480-16*12,100+200*i+5,480);
}
for(i=0;i<plate_height[0];i++)
{
int wide;
wide=hole[0][i]*20;
drawplate(100,480-(i+1)*16,wide);
}
hanoi(m,1,2,3);
getch();
closegraph();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -