比较好理解的A*解决八数码的代码

/*
 * =====================================================================================
 *
 * Filename: eightnum.c
 *
 * Description: 8ÊýÂëÎÊÌ⣚AËã·š£©
 *
 * Version: 1.0
 * Created: 2008-11-4 19:43:19
 * Revision: none
 * Compiler: gcc
 *
 * Author: MneeHNXM (Mnee), [email protected]
 * Company: HeNan University
 *
 * =====================================================================================
 */

#include    <stdlib.h>
#include    <stdio.h>

typedef struct Node {
    int num[9]; //ÆåÅÌ׎̬

    int deepth; //ÅÉÉúµÄÉî¶È g(n)

    int diffnum; //²»ÔÚλµÄÊýÄ¿ h(n)

    int value; //ºÄÉ¢Öµ f(n)=g(n)+h(n)

    struct Node * pre;
    struct Node * next;
    struct Node * parent;
}numNode;                /* ---------- end of struct numNode ---------- */

int origin[9]; //ÆåÅ̳õʌ׎̬

int target[9]; //ÆåÅÌÄ¿±ê׎̬

int numNode_num,total_step;
numNode *open,*close; //Open±íºÍClose±í


numNode *create_numNode()
{
    return (numNode *)malloc(sizeof(numNode));
}
numNode *open_getfirst(numNode *head); //·µ»ØµÚÒ»Ï²¢ŽÓOpen±íÖÐÉŸ³ý

void open_insert(numNode *head,numNode *item); //ÏòOpen±íÖаŽÐò²åÈëÐœڵã

void close_append(numNode *head,numNode *item); //ÏòClose±íÖвåÈëÐœڵã

int expand(numNode *item); //À©Õ¹œÚµã

int print_result(numNode *item); //ŽòÓ¡œá¹û

numNode *copy_numNode(numNode *orgin);
char isNewNode(numNode *open,numNode *close,int num[9]);
                                                //ÊÇ·ñÔÚOpen±í»òClose±íÖÐ

void print_num(int num[9]); //ŽòÓ¡ÆåÅÌ׎̬

int diff(int num[9]); //Çó²»ÔÚλÆå×ӵĞöÊý

void init(); //³õÊŒ»¯£¬»ñµÃÆåÅ̳õʌ׎̬ºÍÄ¿±ê׎̬

void swap(int *a,int *b);
int operate(int num[],int op);
void free_list(numNode *head);

/*
 * === FUNCTION ======================================================================
 * Name: Ö÷º¯”µ
 * Description: ³ÌÐòÈë¿Ú
 * =====================================================================================
 */
    int
main ( int argc, char *argv[] )
{
    //³õÊŒ»¯Open±íºÍClose±í

    open=create_numNode();
    close=create_numNode();
    open->pre=open->next=close->pre=close->next=NULL;

    init(); //ÓÉÓû§ÊäÈë³õÊŒºÍÄ¿±ê׎̬


    //³õÊŒ»¯³õÊŒœÚµã

    numNode *p1;
    p1=create_numNode();
    p1->parent=NULL;
    p1->deepth=0;
    int i=0;
    for ( i=0; i<9; i++)
    {
        p1->num[i]=origin[i];
    }

    open_insert(open,p1);

    numNode_num=1;
    p1=open_getfirst(open);

    while (p1!=NULL)
       {
        close_append(close,p1);

        if(expand(p1))
            return EXIT_SUCCESS;

        p1=open_getfirst(open);
    }

    printf("No solution!\n");
    return EXIT_SUCCESS;
}                /* ---------- end of function main ---------- */

    void
init ( )
{
    while(1)
    {
        printf("Please input opriginal status:\nFor example:123456780 stands for\n"
                "1    2    3\n"
                "4    5    6\n"
                "7    8    0\n");

        char temp[10];
        scanf("%s",temp);
        int i=0;
        for ( i=0;i<9 && temp[i]-'0'>=0 && temp[i]-'0'<=8; i++)
           {
            origin[i]=temp[i]-'0';
        }

        printf("Please input target status:\n");
        scanf("%s",temp);
        int j=0;
        for ( j=0; j<9 && temp[j]-'0'>=0 && temp[j]-'0'<=8; j++)
         {
            target[j]=temp[j]-'0';
        }
        if ( i==9&&j==9)
        {
            break;
        }
    }
}        /* ----- end of function init ----- */

    void
open_insert (numNode *head,numNode *item)
{
    numNode *p,*q;
    p=head->next;
    q=head;
    while ( p!=NULL && item->value > p->value )
       {
        q=p;
        p=p->next;
    }
    q->next=item;
    item->pre=q;
    item->next=p;
    if(p!=NULL)
    {
        p->pre=item;
    }
}        /* ----- end of function open_insert ----- */

    numNode *
open_getfirst (numNode *head)
{
    numNode *p;

    if ( head->next == NULL )
       {
        return NULL;
    }
    p=head->next;
    head->next=p->next;

    if ( p->next != NULL )
       {
        p->next->pre=head;
    }
    p->pre=NULL;
    p->next=NULL;
    return p;
}        /* ----- end of function open_getfirst ----- */

    void
close_append (numNode *head,numNode *item)
{
    item->next=head->next;
    item->pre=head;
    head->next=item;

    if ( item->next!=NULL )
       {
        item->next->pre=item;
    }
}        /* ----- end of function close_append ----- */

    int
expand (numNode *p1)
{
    numNode * p2;
    int op=1;
    for ( op=1; op<=4; op++)
       {
        p2=copy_numNode(p1);
        operate(p2->num,op);
        if(isNewNode(open,close,p2->num)=='N')
        {
            p2->parent=p1;
            p2->deepth=p1->deepth+1;
            p2->diffnum=diff(p2->num);
            p2->value=p2->deepth+p2->diffnum;
            if(p2->diffnum==0)
            {
                total_step=print_result(p2);
                printf("Total step: %d\n",total_step);
                free_list(open);
                free_list(close);
                return 1;
            }
            else
            {
                numNode_num++;
                open_insert(open,p2);
            }
        }
        else
            free(p2);
    }
    return 0;
}        /* ----- end of function expand ----- */

    int
operate(int m[], int op)
{
    int blank;
    blank=0;
    while (m[blank]!=0 && blank<9 )
        ++blank;
    if (blank==9)
        return 1;

    switch (op) {
        case 1: /* up */
            if (blank>2)
                swap(m+blank,m+blank-3);
            break;
        case 2: /* down */
            if (blank<6)
                swap(m+blank,m+blank+3);
            break;
        case 3: /* left */
            if (blank!=0 && blank!=3 && blank!=6)
                swap(m+blank,m+blank-1);
            break;
        case 4: /* right */
            if (blank!=2 && blank!=5 && blank!=8)
                swap(m+blank,m+blank+1);
            break;
        default : return 1;
    }
    return 0;
}

    void
swap(int *a, int *b)
{
    int c;
    c=*a;
    *a=*b;
    *b=c;
}
    numNode *
copy_numNode (numNode *origin)
{
    numNode *p;
    p=create_numNode();
    p->deepth=origin->deepth;
    p->diffnum=origin->diffnum;
    p->value=origin->value;
    int i;
    for ( i=0; i<9; i++)
       {
        (p->num)[i]=(origin->num)[i];
    }

    return p;
}        /* ----- end of function copy_numNode ----- */

    int
diff (int num[9])
{
    int i,diffnum=0;
    for(i=0;i<9;i++)
        if(num[i]!=target[i])
            diffnum++;

    return diffnum;
}        /* ----- end of function diff ----- */
    char
isNewNode (numNode *open,numNode *close,int num[9])
{
    numNode *p;
    int i=0;

    p=open->next;    
    while ( p!=NULL )
    {
        for ( i=0; i<9; i++)
         {
            if(p->num[i]!=num[i])
                break;
        }
        if(i==9)
            return 'O'; //Open

        p=p->next;
    }

    p=close->next;
    while ( p!=NULL )
       {
        for ( i=0; i<9; i++) 
        {
            if(p->num[i]!=num[i])
                break;
        }
        if(i==9)
            return 'C'; //Close

        p=p->next;
    }

    return 'N';
}        /* ----- end of function isNewNode ----- */

    void
free_list (numNode *head)
{
    numNode *p,*q;
    p=head->next;

    while ( p!=NULL )
    {
        q=p->next;
        free(p);
        p=q;
    }
    free(head);
}        /* ----- end of function free_list ----- */

    void
print_num (int num[9])
{    
    int i;

    for ( i=0; i<9; i++)
       {
        printf("%d\t",num[i]);
        if((i%3)==2)
            printf("\n");
    }
}        /* ----- end of function print_num ----- */

    int
print_result ( numNode *item)
{
    numNode *p;
    int step;
    p=item;
    if(p!=NULL)
    {
        step=print_result(p->parent);
        printf("\nStep %d:\n",step+1);
        print_num(p->num);
        return step+1;
    }
    else
    {
        return -1;
    }
}        /* ----- end of function print_result ----- */

你可能感兴趣的:(function,list,struct,null,insert,compiler)