Chinaunix首页 | 论坛 | 博客
  • 博客访问: 583004
  • 博文数量: 61
  • 博客积分: 4112
  • 博客等级: 上校
  • 技术积分: 749
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-27 16:20
文章分类

全部博文(61)

文章存档

2016年(1)

2013年(1)

2012年(2)

2010年(1)

2008年(2)

2007年(25)

2006年(29)

分类: C/C++

2006-10-18 10:51:10

 
 
 
 
汉诺塔非递归实现,并用图像演示
 
在Turbo C 或 wintc 下编译才能通过
文件: hanoi.rar
大小: 9KB
下载: 下载
 

#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;
    }
}

阅读(2041) | 评论(1) | 转发(0) |
0

上一篇:三极管特性

下一篇:J2ME概念解析

给主人留下些什么吧!~~

chinaunix网友2008-06-05 21:57:09

aa