汉诺塔非递归实现,并用图像演示
2006-12-27 12:49
267 查看
#include <stdio.h> #include <dos.h> #include <stdio.h> #define JISHU 1 #define OUSHU 2 #define MAX 9 #define LEFT -1 #define RIGHT 1 /* /* ////////////////////////////////////////////////////////// */ */ void init(int plate, char far *p); /* initialize the graph */ void move(int id,int type, char far *p); /* move the plate */ void power(int plate,long power_s[]); /* initialize the power_s[] */ void start(char far *p); /* start moving */ int scan(int base, char far *p); void draw (int base, int length, char far *p); int plate=100; /* the number of the plate */ int type=JISHU; /* 判断plate是奇数还是偶数 */ long power_s[MAX+1]={0}; /* 提前记录间隔数,以降低调用函数的损耗*/ int count = 0; int base_1[31]={0}; int base_2[31]={0}; int base_3[31]={0}; int n_1=0,n_2=0,n_3=0; int time; /* /* /////////////////////////////////////////////////////////////*/ */ void main(void) { char far *p; union REGS r; r.h.al=0x13; r.h.ah=0; int86(0x10,&r,&r) ; p=(char far *)(0xa0000000l); while(plate>MAX || plate<1) { printf("please input the number of the plate(1~%d)",MAX); scanf("%d",&plate); } printf("please input the delay time(0~~~10000):"); scanf("%d",&time); init(plate, p); /* 初始化图形 */ if(plate%2==0) type=OUSHU; else type=JISHU; power(plate,power_s); start(p); /* 开始移动 */ getchar(); } /* /* //////////////////////////////////////////////////// */ */ void start(char far *p) { int temp[1024]; /* 把移动步骤放入temp中*/ long i=1; /* 循环变量 */ long n=1; /* 首次将要移动的盘子 */ for(n=1;n<=plate;n++) { for(i=power_s[n-1]+1;i<power_s[plate]+1;)/* 第n个盘子将在第i次移动 */ { temp[i]=n; i=i+power_s[n]+1; } } for(i=1;i<=power_s[plate];i++) /* 开始移动*/ { move(temp[i],type,p); } } /* ///////////////////////////////// */ /* */ /* */ /* 以下为图像处理部分 */ /* */ /* ///////////////////////////////// */ void init(int plate, char far *p) { int i=0; int k; system("cls"); for (;i<=200; i++) /* 1 */ { *(p+50+320+320*i)=3; } /* 2 */ for (i=0;i<=200; i++) { *(p+160+320+320*i)=3; } for (i=0;i<=200; i++) /* 3 */ { *(p+265+320+320*i)=3; } for (k=0; k<plate; k++) { for (i=k; i<=100-k; i++) { *(p+i+320*(195-4*k))=1; } delay (time); } k=0; for (i=plate;i>=1;i--) { base_1[k++] = i; } n_1 = k-1; } /* /* ///////////////////////////////////////////////////////// */ */ int scan(int base, char far *p) { int x = 0; int y = 0; int x_l; int length = 0; if (base == 1) x = 49; if (base == 2) x = 159; if (base == 3) x = 264; for (y=0; y<=199; y++) /*scan the y*/ { if (*(p+x+320*y) == 1) break; } x_l = x-49; /*scan the length*/ for (;;) { if (*(p+x_l+320*y)==1) break; x_l++; } for (;;) { if (*(p+x_l+320*y)!=0) { *(p+x_l+320*y)=0; delay (time); x_l++; length++; } else break; } length--; return length; } void draw (int base, int length, char far *p) { int x = 0; int y = 0; int x_l; if (base == 1) x = 49; if (base == 2) x = 159; if (base == 3) x = 264; for (y=0; y<=200; y++) /*scan the y*/ { if (*(p+x+320*y) == 1) break; } y-=4; x_l = x-49+(100-length)/2; for (;length>=0;length--) { *(p+x_l+320*y)=1; x_l++; delay (time); } } void move(int id, int type, char far *p) { int base; int length; int direct; if(type==OUSHU) { if(id % 2==0) direct=LEFT; else direct=RIGHT; } else { if(id % 2==0) direct=RIGHT; else direct=LEFT; } if (id == base_1[n_1]) /*which base has the id plate*/ base = 1; else if (id == base_2[n_2]) base = 2; else if (id == base_3[n_3]) base = 3; else { printf ("Guozhen is GuaWaZi/n"); getchar(); exit (0); } length = scan (base, p); /*scan the length of the id plate and disdraw the plate*/ if (base == 1) /*clear the stack*/ n_1--; else if (base == 2) n_2--; else n_3--; if (direct == LEFT) { if (base == 1) base = 3; else base --; } else { if (base == 3) base = 1; else base ++; } draw (base, length, p); /*draw the plate*/ if (base == 1) /*add the stack*/ { n_1++; base_1[n_1]=id; } else if (base == 2) { n_2++; base_2[n_2]=id; } else { n_3++; base_3[n_3]=id; } count++; printf ("/b/b/b/b/b/b%d",count); } /* /* //////////////////////////////////////////////////////////*/ */ void power(int plate,long power_s[]) { int i=1; for(i=1;i<=plate;i++) { power_s[i]=(power_s[i-1]+1)*2-1; } }
相关文章推荐
- python实现汉诺塔(递归实现)
- <C语言>递归思维及其实现-----汉诺塔问题
- 汉诺塔、反向输出问题--利用递归实现
- Java递归实现汉诺塔
- 递归实现 三角数列 Fibonacci数列 汉诺塔
- php实现汉诺塔问题(递归)
- 【数据结构与算法】汉诺塔算法——java递归实现
- 递归:斐波拉契,阿克曼与汉诺塔(php实现)
- 递归:斐波拉契,阿克曼与汉诺塔(php实现)
- 汉诺塔-递归实现
- 递归实现汉诺塔
- 九层汉诺塔递归和非递归模拟演示(C语言)
- 汉诺塔,逆波兰表达式,苹果放置递归实现
- 递归(汉诺塔) Go实现
- 演示如何实现Matplotlib绘图并保存图像但不显示图形的方法
- 汉诺塔递归演示
- 汉诺塔递归的c语言实现(递归)
- 关于递归的总结——汉诺塔、素因数的求解(Python实现)
- 3行核心代码解决汉诺塔问题(C++递归实现)
- 递归实现(汉诺塔+阶乘)