汉诺塔c语言程序代码栈(汉诺塔程序设计c++函数)

http://www.itjxue.com  2023-03-17 20:14  来源:未知  点击次数: 

汉诺塔的C语言代码怎么写啊

/*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(n1||n10)

n=10;/*越界的话n当10处理*/

if(computer)/*如果是自动控制的话输入速度*/

{

printf("please input speed: ");/*输入速度*/

scanf("%d",speed);

}

initgraph(gd,gm,"c:\\tc");

cleardevice();

for(i=0;i3;i++)

num[i].top=-1;/*三个地方的高度开始都为-1*/

for(i=0;in;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语言代码来编写含汉诺塔问题,利用堆栈来实现.求代码

算法思想

对于汉诺塔问题,当只移动一个圆盘时,直接将圆盘从 A 针移动到 C 针。若移动的圆盘为 n(n1),则分成几步走:把 (n-1) 个圆盘从 A 针移动到 B 针(借助 C 针);A 针上的最后一个圆盘移动到 C 针;B 针上的 (n-1) 个圆盘移动到 C 针(借助 A 针)。每做一遍,移动的圆盘少一个,逐次递减,最后当 n 为 1 时,完成整个移动过程。

因此,解决汉诺塔问题可设计一个递归函数,利用递归实现圆盘的整个移动过程,问题的解决过程是对实际操作的模拟。

程序代码

#include stdio.h

int main()

{

int hanoi(int,char,char,char);

int n,counter;

printf("Input the number of diskes:");

scanf("%d",n);

printf("\n");

counter=hanoi(n,'A','B','C');

return 0;

}

int hanoi(int n,char x,char y,char z)

{

int move(char,int,char);

if(n==1)

move(x,1,z);

else

{

hanoi(n-1,x,z,y);

move(x,n,z);

hanoi(n-1,y,x,z);

}

return 0;

}

int move(char getone,int n,char putone)

{

static int k=1;

printf("%2d:%3d # %c---%c\n",k,n,getone,putone);

if(k++%3==0)

printf("\n");

return 0;

}

C语言汉诺塔问题非递归解法代码求大神讲解

int game2()要改为int main()后才可编译运行:

#includestdio.h

#includestdlib.h

#define CSZL 10

#define FPZL 10

typedef struct hanoi

{

int n;

char x,y,z;

}hanoi;

typedef struct Stack //定义栈结点

{

hanoi *base,*top;

int stacksize;

}Stack;

int InitStack(Stack *S)

{

S-base=(hanoi *)malloc(CSZL*sizeof(hanoi)); //申请栈空间

if(!S-base) //若申请不成功返回失败信息

return 0;

S-top=S-base; //置为空栈

S-stacksize=CSZL; //栈结点数

return 1;

}

int PushStack(Stack *S,int n,char x,char y,char z) //入栈

{

if(S-top-S-base==S-stacksize) //若栈已满

{

S-base=(hanoi *)realloc(S-base,(S-stacksize+FPZL)*sizeof(hanoi)); //补充申请,扩充栈空间

if(!S-base) ? //若申请不成功返回失败信息

return 0;

S-top=S-base+S-stacksize; //重置栈顶指针

S-stacksize+=FPZL; //重置栈空间尺寸

}

S-top-n=n; //新结点信息存入栈顶结点

S-top-x=x;

S-top-y=y;

S-top-z=z;

S-top++; //栈中元素加1

return 1;

}

int PopStack(Stack *S,int *n,char *x,char *y,char *z) //出栈

{

if(S-top==S-base) //若栈已空

return 0; //返回出错信息

else

{

S-top--; //栈顶指针减1

*n=S-top-n; //返回出栈元素信息

*x=S-top-x;

*y=S-top-y;

*z=S-top-z;

return 1;

}

}

int EmptyStack(Stack *S) //判定是否空栈

{

if(S-base==S-top)

return 1;

else

return 0;

}

int i=1;

void Move(char x,char z) //打印移动盘子的信息

{

printf("\n\t\t第%d步,%c--%c",i++,x,z);

}

int main() /* 非递归法 */

{

int n;

char x,y,z;

Stack *S;

system("cls"); /*清屏指令*/

S=(Stack *)malloc(sizeof(Stack)); //申请栈空间

if(!S)

return 0;

if(!InitStack(S)) //初始化栈

return 0;

printf("请输入汉诺塔的初始盘子数量以及轴的名称:");

scanf("%d\t%c%c%c",n,x,y,z);

if(!PushStack(S,n,x,y,z)) //要移动的盘子数及各轴名称入栈

return 0;

while(!EmptyStack(S)) //当栈非空时循环

{

if(!PopStack(S,n,x,y,z)) //若空栈则退出循环,否则出栈一个任务

break;

if(n==1) //若只有一个盘子,直接移动

{

Move(x,z);

}

else //有1个以上的盘子时入栈(注意栈的工作特点,是后进先出,所以最先做的事要最后入栈)

{

if(!PushStack(S,n-1,y,x,z)) //将上层的n-1个盘子从y移向z

break;

if(!PushStack(S,1,x,y,z)) //将底层的盘子从x移向z

break;

if(!PushStack(S,n-1,x,z,y)) //将上层的n-1个盘子从x移向y

break;

}

}

free(S-base);

S-base=NULL;

S-top=NULL;

S-stacksize=0;

return 0;

}

(责任编辑:IT教学网)

更多

推荐PHP+MySQL视频文章