递归算法 汉诺塔 演示

作者&投稿:爨荔 (若有异议请与网页底部的电邮联系)
求汉诺塔递归全过程的算法详解图,记得一定要是图释哦!!!~


#include
#include
void
tim(int
k){
/*延时函数*/
int
i;
for(i=0;i<k;i++)
delay(5000);
}
void
move(char
x,char
y,int
n){/*移动*/
tim(n);
printf("%c-->%c
",x,y);}
void
hanoi(int
k,int
n,char
one,char
two,char
three){
if(n==1)
move(one,three,k);
else
{
hanoi(k,n-1,one,three,two);
move(one,three,k);
hanoi(k,n-1,two,one,three);}
}
main(){
int
m,k;
printf("Hello!This
is
hanoi
Wrold!
");
printf("input
the
number(1~10)
of
diskes:");
/*选择盘子个数*/
scanf("%d",&m);
printf("
choose
speed
1,2,3
:");/*选择速度*/
scanf("%d",&k);
printf("The
step
tp
moving
%3d
diskes:
",m);
hanoi(50*k,m,'A','B','C');
/*三个柱分别命名为A,B,C*/
getch();
}
/*还要"最好有图形说明、按键操作和一些程序语句的解释说明"吗?加分先!*/

TurboC 1024×768 真彩色,演示全木质汉诺塔
ESC 退出, 空格切换 自动手动
tc 2.0, 3.0 均可运行

#include <dos.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MINDISK 1
#define MAXDISK 15
#define DISKHEIGHT 30
#define TEXT_BIG_CURSOR 0
#define TEXT_NORMAL_CURSOR 7

typedef struct /*16Mrgb像素类型*/
{
unsigned char b;
unsigned char g;
unsigned char r;
}rgb16M;

typedef struct tagDisks
{
int x;
int y;
int Left;
int Top;
int Lenth;
int Height;
int Tag;
int Cylinder1;
int Cylinder2;
int Cylinder3;
}Disks;

void DrawPillar(int number, int bottom);
void DrawDisk(int x, int y, int lenth, int height);
void HanoiMove(int n, int X, int Y, int Z);
void MoveDisk(int n, int DiskFrom, int DiskTo);
void SetXY(Disks *disk, int nX, int nY, int cleardisk);

int disk_num;
int automove = 1;
int toquit = 0;
int delayperiod;
int CylinderHeight[3];
Disks disks[MAXDISK + 1];
rgb16M backcolor = {140, 197, 227}, backcolor1 = {34, 95, 149};
rgb16M woodcolor[2][4] = {{{180, 223, 240},{138, 205, 230},{98, 188, 222},{58, 171, 214}},{{2, 96,132},{4,110,151},{3, 121, 166},{4, 135, 183}}};
rgb16M bordercolor = {52, 128, 203};

long woodfillcolor[35][30] = {
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1}};

unsigned char set_SVGA_mode(int vmode)
{union REGS r;
r.x.ax=0x4f02;
r.x.bx=vmode;
int86(0x10,&r,&r);
return(r.h.ah);
}

void hide_text_cursor(void)
{union REGS r;
r.h.ah=1;
r.h.ch=32;
int86(0x10,&r,&r);
}

void selectpage(register char page)
{union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page;
int86(0x10,&r,&r);
}

void show_text_cursor(char size)
{union REGS r;
r.h.ah=1;
r.h.cl=size;
r.h.ch=7;
int86(0x10,&r,&r);
}

unsigned int get_SVGA_mode()
{union REGS r;
r.x.ax=0x4f03;
int86(0x10,&r,&r);
return(r.x.bx);
}

unsigned int Vmode=0;
void init16M()
{Vmode=get_SVGA_mode();
hide_text_cursor();
if(set_SVGA_mode(0x118))
{printf("\nSorry, Your graphics driver does not supplied.\n");
show_text_cursor(TEXT_NORMAL_CURSOR);
exit(0);
}
}

void close16M()
{
union REGS r;
r.h.ah = 0;
r.h.al = 3;
int86(0x10, &r, &r);
r.h.ah = 0x05;
r.h.al = 0;
int86(0x10, &r, &r);
}

void putpoint16M(int x, int y, rgb16M color)
{
static int nowpage = 0, lastpage = 50;
static long temp;
unsigned char far *p = (unsigned char *)0xa0000000;
temp = y;
x <<= 2;
temp <<= 12;
temp += x;

nowpage = temp >> 16;
temp &= 0xffff;
if (nowpage != lastpage)
{
static union REGS r;
r.x.ax = 0x4f05;
r.x.bx = 0;
r.x.dx = nowpage;
int86(0x10, &r, &r);
lastpage = nowpage;
}

*((rgb16M *)(p + temp)) = color;
}

void line(int startx, int starty, int endx, int endy, rgb16M color)
{
register int t, distance;
int x = 0, y = 0;
int delta_x, delta_y;
int incx, incy;

delta_x = endx-startx;
delta_y = endy-starty;

if (delta_x > 0) incx = 1;
else if(delta_x == 0) incx = 0;
else incx = -1;
if(delta_y > 0) incy = 1;
else if (delta_y == 0) incy = 0;
else incy = -1;

if (delta_x < 0) delta_x = -delta_x;
if (delta_y < 0) delta_y = -delta_y;

if(delta_x > delta_y) distance = delta_x;
else distance = delta_y;

for (t = 0; t <= distance + 1; t++){
putpoint16M(startx, starty, color);
x += delta_x;
y += delta_y;
if (x > distance){
x -= distance;
startx += incx;
}
if(y > distance){
y -= distance;
starty += incy;
}
}
}

void fillbox(int startx, int starty, int endx, int endy, rgb16M color)
{
int i, j;

if (startx > endx)
{
i = startx;
startx = endx;
endx = i;
}
if (starty > endy)
{
i = starty;
starty = endy;
endy = i;
}
if (startx < 0)
startx = 0;
if (endx > 1023)
endx = 1023;
if (starty < 0)
starty = 0;
else if (endy > 767)
endy = 767;

for (j = starty; j <= endy; j++)
for (i = startx; i <= endx; i++)
putpoint16M(i, j, color);
}

int main(void)
{
int i, j;
int left, top;
int width = 975;
int height = 600;

reinnum:;
printf("input the number of disk(%d-%d, 0 to quit): ", MINDISK, MAXDISK);
delayperiod = 500;
scanf("%d", &disk_num);
if (disk_num == 0)
return 0;
if (disk_num < MINDISK || disk_num > MAXDISK)
goto reinnum;
printf("select play style(0:auto, 1:manual): ");
scanf("%d", &automove);
if (automove != 0)
automove = 1;
if (automove == 0)
{
reintime:;
printf("input the delay period(100-5000): ");
scanf("%d", &delayperiod);
if (delayperiod < 100 || delayperiod > 5000)
goto reintime;
}

init16M();

left = (1024 - width) >> 1;
top = (768 - height) >> 1;;

fillbox(left, top, left + width, top + height, backcolor1);
fillbox(left - 10 , top - 12, left - 2, top + height + 13, bordercolor);
fillbox(left + width + 2, top - 12, left + width + 10, top + height + 13, bordercolor);
fillbox(left, top - 12, left + width, top - 2, bordercolor);
fillbox(left, top + height + 2, left + width, top + height + 13, bordercolor);

for (i = 0; i < 3; i++)
CylinderHeight[i] = 0;

disks[1].Cylinder1 = left + width / 6;
disks[1].Cylinder2 = left + width / 2;
disks[1].Cylinder3 = left + width / 6 * 5;
DrawPillar(1, 0);
DrawPillar(2, 0);
DrawPillar(3, 0);

DrawDisk(disks[1].Cylinder1 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder2 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder3 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);

for (i = disk_num; i >= 1; i--)
{
disks[i].x = 1;
disks[i].y = i;
disks[i].Cylinder1 = left + width / 6;
disks[i].Cylinder2 = left + width / 2;
disks[i].Cylinder3 = left + width / 6 * 5;
disks[i].Height = DISKHEIGHT;
disks[i].Tag = i;
disks[i].Lenth = (width / 4 / disk_num * i) + 7;
SetXY(&disks[i], 1, CylinderHeight[0], 0);
CylinderHeight[0]++;
}

getch();
HanoiMove(disk_num, 1, 2, 3);
getch();

close16M();

return 0;

}

void DrawPillar(int number, int bottom)
{
int x, y = 110;
int lenth = 8, height;
int i, j, m, n;

if (number == 1)
x = disks[1].Cylinder1 - 4;
else if (number == 2)
x = disks[1].Cylinder2 - 4;
else
x = disks[1].Cylinder3 - 4;

if (bottom == 0)
height = 575;
else
height = bottom;

m = 0;
n = 0;
for (i = y + 4; i <= y + height - 1 && i < bottom; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}

woodcolor[0][3] = backcolor;
for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 1, woodcolor[1][i]);
}

}

void DrawDisk(int x, int y, int lenth, int height)
{
int i, j, m, n;
woodcolor[0][3] = backcolor;

for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - i - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + i + 1, y + height + i - 4, x + lenth - 1, y + height + i - 4, woodcolor[1][3 - i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 4, woodcolor[1][i]);
}

m = 0;
n = 0;
for (i = y + 4; i <= y + height - 5; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}

}

void MoveDisk(int n, int DiskFrom, int DiskTo)
{
if (toquit == 1)
return;

SetXY(&disks[n], DiskTo, CylinderHeight[DiskTo - 1], 1);
CylinderHeight[DiskFrom - 1]--;
CylinderHeight[DiskTo - 1]++;

}

void SetXY(Disks *disk, int nX, int nY, int cleardisk)
{
if (cleardisk == 1)
{
fillbox(disk->Left, disk->Top, disk->Left + disk->Lenth - 1, disk->Top + disk->Height - 1, backcolor1);
DrawPillar(disk->x, DISKHEIGHT - 35 + 599 - disk->Height * CylinderHeight[disk->x - 1] - 1);
}
disk->x = nX;
disk->y = nY;
switch (nX)
{
case 1 :
disk->Left = disk->Cylinder1 - disk->Lenth / 2;
break;

case 2 :
disk->Left = disk->Cylinder2 - disk->Lenth / 2;
break;

case 3 :
disk->Left = disk->Cylinder3 - disk->Lenth / 2;
break;

default :
;
}

disk->Top = 35 - DISKHEIGHT + 638 - disk->Height * disk->y;
DrawDisk(disk->Left, disk->Top, disk->Lenth, disk->Height);

}

void HanoiMove(int n, int X, int Y, int Z)
{
int a;
if (n == 0 || toquit == 1)
return;
HanoiMove(n - 1, X, Z, Y);
MoveDisk(n, X, Z);

if (toquit == 1)
return;

if (automove == 1)
{
a = getch();
if (a == 0)
{
a = getch();
}
else if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
else
{
if (kbhit())
{
a = getch();
if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
delay(delayperiod);
}

HanoiMove(n - 1, Y, X, Z);

}

#include <GL/glaux.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

float WIDTH = 400;
float HEIGHT = 400;
GLboolean motion = GL_TRUE;
GLboolean back_wall = GL_FALSE;
GLint xangle = 0, yangle = 0;
GLint xlangle = 0, ylangle = 0;

#define other(i,j) (6-(i+j))
#define wallz -(WIDTH/2)
#define DISK_HEIGHT 20
int NUM_DISKS = 11;
#define CONE NUM_DISKS+1
#define WALL CONE + 1
#define HANOI_SOLVE 0
#define HANOI_QUIT 1
#define HANOI_LIGHTING 2
#define HANOI_WALL 3
#define HANOI_FOG 4

GLfloat lightOneDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightOnePosition[] = {200.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightOneColor[] = {1.0f, 1.0f, 0.5f, 1.0f};

GLfloat lightTwoDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightTwoPosition[] = {600.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightTwoColor[] = {1.0f, 0.0f, 0.3f, 1.0f};

GLfloat lightZeroPosition[] = {400.0f, 200.0f, 300.0f, 1.0f};
GLfloat lightZeroColor[] = {0.6f, 0.6f, 0.0f, 0.0f};

GLfloat diskColor[] = {1.0f, 1.0f, 1.0f, 0.8f},
poleColor[] = {1.0f, 0.2f, 0.2f, 0.8f};

typedef struct stack_node {
int size;
struct stack_node *next;
} stack_node;

typedef struct stack {
struct stack_node *head;
int depth;
} stack;

stack poles[4];

void push(int which, int size)
{
stack_node *new_node = malloc(sizeof(stack_node));
if (!new_node) {
printf("out of memory!\n");
exit(-1);
}
new_node->size = size;
new_node->next = poles[which].head;
poles[which].head = new_node;
poles[which].depth++;
}

int pop(int which)
{
int retval = poles[which].head->size;
stack_node *temp = poles[which].head;
poles[which].head = poles[which].head->next;
poles[which].depth--;
free(temp);
return retval;
}

typedef struct move_node {
int t, f;
struct move_node *next;
struct move_node *prev;
} move_node;

typedef struct move_stack {
int depth;
struct move_node *head, *tail;
} move_stack;

move_stack moves;

void GenLists(void)
{
int i;
for (i = 0; i < 4; i++) {
poles[i].head = NULL;
poles[i].depth = 0;
}
moves.head = NULL;
moves.tail = NULL;
moves.depth = 0;

for (i = 1; i <= NUM_DISKS; i++) {
glNewList(i, GL_COMPILE);
{
auxSolidTorus(DISK_HEIGHT / 2.0f, 4 * i);
}
glEndList();
}
glNewList(CONE, GL_COMPILE);
{
auxSolidCone(8.0f, (NUM_DISKS + 1) * DISK_HEIGHT);
}
glEndList();
}

void Init(void)
{
glViewport(0, 0, (int)WIDTH, (int)HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -10000, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glClearDepth(1.0);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightOneDirection);
glEnable(GL_LIGHT1);

glLightfv(GL_LIGHT2, GL_POSITION, lightTwoPosition);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightTwoColor);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightTwoDirection);
glEnable(GL_LIGHT2);

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}

void mpop(void)
{
move_node *temp = moves.head;
moves.head = moves.head->next;
free(temp);
moves.depth--;
}

void move(int t, int f)
{
move_node *new = malloc(sizeof(move_node));
if (!new) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
new->t = t;
new->f = f;
new->next = NULL;
new->prev = moves.tail;
if (moves.tail)
moves.tail->next = new;
moves.tail = new;
if (!moves.head)
moves.head = moves.tail;
moves.depth++;
}

void DrawPost(int xcenter)
{
glPushMatrix();
{
glTranslatef((float)xcenter, 0.0f, 0.0f);
glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
glCallList(CONE);
} glPopMatrix();
}

void DrawPosts(void)
{
glColor3fv(poleColor);
glLineWidth(1.0f);
glMaterialfv(GL_FRONT, GL_DIFFUSE, poleColor);
DrawPost((int)WIDTH / 4);
DrawPost(2 * (int)WIDTH / 4);
DrawPost(3 * (int)WIDTH / 4);
}

void DrawDisk(int xcenter, int ycenter, int size)
{
glPushMatrix();
{
glTranslatef((float)xcenter, (float)ycenter, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glCallList(size);
} glPopMatrix();
}

void DrawDooDads(void)
{
int i;
stack_node *temp;
int xcenter, ycenter;
glColor3fv(diskColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diskColor);
for (i = 1; i <= 3; i++) {
xcenter = i * (int)WIDTH / 4;
ycenter = DISK_HEIGHT * poles[i].depth - DISK_HEIGHT / 2;
for (temp = poles[i].head; temp; temp = temp->next) {
ycenter -= DISK_HEIGHT;
DrawDisk(xcenter, ycenter, temp->size);
}
}
}

static void hanoi(int n, int f, int t)
{
int o;

if (n == 1) {
move(t,f);
return;
}
o = other(f, t);
hanoi(n - 1, f, o);
hanoi(1, f, t);
hanoi(n - 1, o, t);
}

GLfloat wallcolor[] = {0.0f, 0.3f, 1.0f, 1.0f};

void CALLBACK draw(void)
{
int t, f;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xlangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)ylangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
}
glPopMatrix();

glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)yangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
DrawPosts();
DrawDooDads();
}
glPopMatrix();
if (motion && moves.depth) {
t = moves.head->t;
f = moves.head->f;
push(t, pop(f));
mpop();
}
auxSwapBuffers();
}

void CALLBACK update(void)
{
draw();
}

void CALLBACK reshape(int w, int h)
{
glViewport(0,0,w,h);
}

int oldx, oldy;

GLboolean leftb = GL_FALSE, middleb = GL_FALSE;

void main(int argc, char *argv[])
{
int i;
auxInitPosition(0, 0, (int)WIDTH, (int)HEIGHT);
auxInitDisplayMode(AUX_RGBA | AUX_DOUBLE | AUX_DEPTH);
auxInitWindow("Hanoi");
GenLists();
Init();

for (i = 0; i < NUM_DISKS; i++)
push(1, NUM_DISKS - i);
hanoi(NUM_DISKS, 1, 3);
auxReshapeFunc(reshape);
auxIdleFunc(update);
auxMainLoop(draw);
}

整理分析结果:把第1步中化简问题的条件作为递归 结束条件,将第3步分析得到的算法作为递归算法。
定义函数movedisc( n,fromneedle,toneedle,usingneedle)。将 fromneedle 杆上的 N 个圆盘,借助 usingneedle 杆,移动到 toneedle 杆上。
移动N个圆盘的递归算法描述如下:
movedisc ( n,fromneedle,toneedle,usingneedle )
{ if ( n==1 ) 将 n 号圆盘从 fromneedle 上移到 toneedle;
else {
① movedisc ( n-1,fromneedle,usingneedle,toneedle )
② 将 n 号圆盘从 fromneedle 上移到 toneedle;
③ movedisc ( n-1,usingneedle,toneedle,fromneedle )
}
}

下面是程序
int i=0; /* 移动圆盘数量计数器 */
main( )
{ unsigned n;
scanf ("%d", &n);
movedisc ( n,’a’,’b’,’c’); /* 将A上的N个圆盘借助C将移动到B上 */
printf( "\t Total: %d\n", i );
}
movedisc ( n, fromneedle, toneedle, usingneedle )
unsigned n;
char fromneedle, toneedle, usingneedle;
{ if ( n == 1 )
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
else {
movedisc ( n-1, fromneedle, usingneedle, toneedle );
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
movedisc ( n-1, usingneedle, toneedle, fromneedle );
}
}

不急的话,周末给你写一个.




晋州市19556547492: 汉诺塔问题的递归算法流程图 -
延素清火: 关键是第一步移法,奇数层的说,3层在第一柱,后两根柱数数:123.所以,第一块应放在第二根柱,4层,第一块放第三柱............奇数层第一块放第二柱,偶数层第一块放第三柱.

晋州市19556547492: 汉诺塔递归算法
延素清火: 汉诺塔 递归算法 Hanoi(int n,char Start,Middle,End) begin if n=1 then 输出Start-&gt;End else begin Hanoi(n-1,Start,End,Middle); //要把Start的盘子借助middle移动到End 先把n-1个盘子由start移到middle //这步做完后 Start上 n-1个盘子移到中转盘 Middle上 输出 Start-&gt;End; //把Start上最后一个盘子移到End Hanoi(n-1,Middle,Start,End); end end

晋州市19556547492: 求C汉诺塔递归过程详解 -
延素清火: 解决汉诺塔的基本思想是先把n个盘子除了最下面的盘子以外的所有盘子从第一根柱子(初始柱子)移动到中间那个柱子上(辅助柱子),然后把最下面的盘子移动到最后一根柱子上(目标柱子).最后把剩下的盘子移动到目标柱子上.这样,...

晋州市19556547492: 求汉诺塔C递归算法详细解答 -
延素清火: Hanoi塔问题, 算法分析如下,设A上有n个盘子.如果n=1,则将圆盘从A直接移动到C.如果n=2,则:(1)将A上的n-1(等于1)个圆盘移到B上;(2)再将A上的一个圆盘移到C上;(3)最后将B上的n-1(等于1)个圆盘移到C上.如果n=...

晋州市19556547492: 什么是汉诺塔 如何利用递归调用 -
延素清火: 汉诺塔(又称河内塔)问题是印度的一个古老的传说.开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒,第一根上面套着64个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从这根棒搬...

晋州市19556547492: 汉诺塔n=4(4个盘)c语言递归编程代码 -
延素清火: /**************************** 汉诺塔的算法就3个步骤:第一,把a上的n-1个盘通过c移动到b.第二,把a上的最下面的盘移到c.a成了空的.第三,因为n-1个盘全在b上了,所以把b当做a.重复以上步骤就好了.所以算法看起来就简单多了.*********...

晋州市19556547492: 汉诺塔递归算法 -
延素清火: 1个只要1次2个碟子要3次3个要7次归纳法可以推得复杂度为2^n-1这个可以证明的,只是证明很复杂.

晋州市19556547492: 汉诺塔问题算法 -
延素清火: 用递归实现:#include void Towers(int n, char fromPeg, char auxPeg, char toPeg) { if (1 == n) //递归出口 { coutreturn; } //把n-1个盘子从fromPeg借助toPeg移动到auxPeg Towers(n - 1, fromPeg, toPeg, auxPeg); //把盘子n由fromPeg直接移动到...

晋州市19556547492: 有没有谁有用递归算法演示汉诺塔过程的C语言程序?谢谢了! -
延素清火: #include<stdio.h> #include<stdlib.h> void tim(int k){ /*延时函数*/ int i; for(i=0;i<k;i++) delay(5000); } void move(char x,char y,int n){/*移动*/ tim(n); printf("%c-->%c\n",x,y);} void hanoi(int k,int n,char one,char two,char three){ if(n==1) move(one,three,...

晋州市19556547492: 谁能告诉我关于汉诺塔递归算法的详细运行步骤(c\c++)? -
延素清火: 汉诺塔的规则是把N个盘子从A柱挪到C柱(假设是这样) 那末,我们要做的就是把N-1个盘子从A柱挪到B柱,再把1个盘子从A柱挪到C柱,再把N-1个盘子从B柱挪到C柱. 当运行到N-1的时候,N就代表N-1,这时再把N-2个盘子从开始柱挪到临时柱,再把1个主子从开始柱挪到结束柱,再把n-2个柱子从临时柱挪到结束柱.不停的调用自身,直到调用的程序的N=1的时候…… 说了这些,不知道阁下懂不懂.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网