网站地图 | 最新更新 | 卜安居 - 手相面相网
你的位置:首页 > 识人术 » 正文

汉罗塔动态演示算法(汉罗塔函数)

2023-08-31 00:56:28 | 1019人围观 | 张树林撰历史

汉诺塔程序设计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,"D:\\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();
}

汉诺塔c语言程序代码

学习分享基于python实现的汉诺塔递归实现与分析内容偏水

#include <graphics.h>
#include<stdio.h>
#include <conio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#define N 10        //定义盘数
RECT r = {240, 0, 460, 240};
TCHAR step[5];
int stepn=0;
int pan[3];
void dizuo()
{
    setlinestyle(PS_SOLID,NULL,4);
    line(20,400,160,400);
    line(90,200,90,400);
    line(220,400,360,400);
    line(290,200,290,400);
    line(420,400,560,400);
    line(490,200,490,400);
}
//初始化汉诺塔
void hanoi_draw()
{
    int i;
    setlinestyle(PS_SOLID,NULL,12);
    for(i=0;i<=(N-1)*7;i+=7)
    {
        setcolor(HSLtoRGB(12*15, 1, 0.5));
        line(20+i,390-2*i,160-i,390-2*i);
    }
}
//移渣谈动
void move(int n,int one,int three)
{
    sprintf(step, _T("%d"),++stepn);   
    outtextxy(340,140,step);
    pan[one-1]-=1;
    pan[three-1]+=1;
    setlinestyle(PS_SOLID,NULL,12);
    clearrectangle((one-1)*200+(N-n)*7,390-(pan[one-1])*14-6,(one-1)*200+180-(N-n)*7,390-(pan[one-1])*14+6);
    dizuo();
    setlinestyle(PS_SOLID,NULL,12);
    line((three-1)*200+(N-n)*7+14,390-(pan[three-1]-1)*14,(three-1)*200+166-(N-n)*7,390-(pan[three-1]-1)*14);
}
//汉诺塔算法
void hanoi(int n,int one,int two,int three)
{
    if (n==1)
    {
        move(n-1,one,three);
    }
    else
    {
      迟颤  hanoi(n-1,one,three,two);
        Sleep(2000);
        move(n-1,one,three);
        Sleep(2000);
 码梁败       hanoi(n-1,two,one,three);
    }
}
void main()
{
    initgraph(640,480);     //初始化图形
    // 设置背景色为蓝色
    setbkcolor(BLUE);
    // 用背景色清空屏幕
    cleardevice();
    settextstyle(30, 0, _T("宋体"));
        drawtext(_T("汉诺塔动画演示"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    settextstyle(16, 0, _T("宋体"));
    outtextxy(300,140,"step:");
    pan[0]=N;       //第一个座初始有3个盘
    dizuo();        //画出底座
    hanoi_draw();   //初始化汉诺塔
    Sleep(2000);
    hanoi(N,1,2,3);
    getch();
    closegraph;
}

Top