您的位置:首页 > 其它

一个用于练习链表的“跳马”程序

2006-06-26 10:30 162 查看
# include <stdio.h>
/****************************************
*程序名称: 跳  马                      *
*程序作者: flyli(ProgramFan)           *
*程序版本: 1.00                        * 
*完成时间:2006.6.1                     *
*修改时间:                             *
*程序说明:此程序所完成的事情是算出中国 *
*          象棋中的“马”从一个位置调到 *
*          另一个位置的所有算法,要求马 *
*          只能向右走不能向左走.        *
****************************************/
unsigned XNum = 10;
unsigned YNum = 10;
unsigned Jadge,MidSate = 0,ALL=0,x,y;      //
struct Information *PHead,*P1,*P2,*P3;
void New(int X,int Y,int Sate);
void Del_Free(void);
char SetStart(int X,int Y,int Sate);
struct Information   //一个结构体,拥有X,Y坐标信息,一个链节信息和2个指针
{
    int X;
    int Y;
    int Sate;
    struct Information * next;
    struct Information * back;
};

int main(void)
{
    PHead = P1 = P2 = (struct Information *)malloc(sizeof(struct Information));
    //建立一个空的结构(主要用于判断程序的结束条件)
    P2 = (struct Information *)malloc(sizeof(struct Information));
    P2->X = 0;
    P2->Y = 0;
    P2->next = 0;
    P2->back = P1;
    P1->next = P2;
    P1 = P2;
    P2->Sate = 1;
    while(SetStart(P2->X,P2->Y,MidSate))
    {;}
    while(P2 != PHead)
    {
        while(SetStart(P2->X,P2->Y,MidSate))
        {
            MidSate=1;
        }  
        Del_Free();
        while(MidSate == 5)
        {
            Del_Free();
        }         
    }
    printf("%d",ALL);
   
    system("pause");
    return 0;   
}

/***********************************
*函数说明:建立一个新的链          *
*函数输入:新的链中的字符位置      *
***********************************/
void New(int X,int Y,int Sate)
{
   P2 = (struct Information *)malloc(sizeof(struct Information)); //开辟个内存空间            
   P1->next = P2;                    //将上个指针连到此结构体
   P2->back = P1;                    //将这个空间的回指针指向上个指针
   P1 = P2;                          //把p1,p2都指到新的空间
   P2->X = X;                        //对新空间进行赋值
   P2->Y = Y;
   P2->Sate = Sate;                   
   P2->next = 0;                      //将next指针赋值为0
}

/***********************************
*函数说明:释放一个链 (最后的那个) *
*函数输入:新的链中的字符位置      *
***********************************/
void Del_Free(void)
{
   
    MidSate = P2->Sate+1;
    P2 = P2->back;            //将p2指回上一结构体
    P1 = P2;                  //将P1也指回
    free(P2->next);           //把后面的那个结构体的内存空间释放掉
    P2->next = 0;             //对next指针进行赋0 
}

/***********************************
*函数说明: 用于探索下一位置的函数 *
*函数输入: 要探索的起始位置       *
***********************************/
char SetStart(int X,int Y,int Sate)
{
    if (Sate ==1)
        goto X1;
    else if (Sate ==2)
        goto X2;
    else if (Sate ==3)
        goto X3;
    else if (Sate ==4)
        goto X4;
        x=X;y=Y;
X1: if(X+1<XNum && Y+2<YNum)              //当马跳后不会超出棋盘时
    {
        New(X+1,Y+2,1);                      //继续跳
        return 1;                          //判断变量为真
    }
X2: if(X+1<XNum && Y-2>=0)
    {
        New(X+1,Y-2,2); 
        return 1;        
    }
X3: if(X+2<XNum && Y+1<YNum)
    {
        New(X+2,Y+1,3);
        return 1;
    } 
X4: if(X+2<XNum && Y-1>=0)
    {
        New(X+2,Y-1,4);
        return 1;
    }  
    x = P2->X;y = P2->Y;
    if(x == XNum-1 && y == YNum-1 )  //
    {
        P3 = PHead->next;
        while(P3->next != 0)
        {
            printf("%d,%d/n",P3->X,P3->Y);
            P3 = P3->next;                   
        }
        printf("%d,%d/n/n",XNum-1,YNum-1);
        ALL++;
                
    }
        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct include system 算法
相关文章推荐