自己这两天看算法书,写的一个关于八数码的A*算法,请各位指点指点!
有不足的地方请尽量详细的指出来,不胜感激!!!
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct Element
{
    int zero;
    int father;
    int temp[9];
}e[100];
int up,down,left,right;
int start[9],end[9],assistion[9];
int p;//p是估计值
void print(int n)
{
    int i;printf("第%d个:\n",n);
    for(i=0;i<9;i++)
    {
        if(i%3==0)printf("\n");
        printf("%d  ",e[n].temp[i]);
    }
    printf("\n*************************\n");
}
void findDirect(int n)//找可行的方向
{
    switch(n)
    {
        case 0:{right=down=0;left=up=1;break;}
        case 1:{right=left=up=1;down=0;break;}
        case 2:{left=down=0;right=up=1;break;}
        case 3:{right=0;left=up=down=1;break;}
        case 4:{left=up=down=right=1;break;}
        case 5:{right=down=up=1;left=0;break;}
        case 6:{right=up=0;left=down=1;break;}
        case 7:{up=0;left=down=right=1;break;}
        case 8:{up=left=0;down=right=1;break;}
    }
    
}
int findFunction(int *start,int *end)//求估算值
{
    int num=0;
    int i,j;
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(start[i]==end[j] && start[i]!=0 && end[j]!=0)
            {
                if(i!=j){
                    if((abs(j-i))%3==0)num++;else
                    num += (abs(j-i))%3;
                }
            }
        }
    }
    return num;
}
int Up(int n,int k,int m)
{
    int i,num;
    for(i=0;i<9;i++)
    {
        e[m].temp[i]=e[k].temp[i];
    }
    e[m].temp[n]=e[m].temp[n+3];
    e[m].temp[n+3]=0;
    e[m].zero=n+3;
    for(i=0;i<9;i++)
    {
        assistion[i]=e[m].temp[i];
    }
    num=findFunction(assistion,end);
    return num;
}
int Down(int n,int k,int m)
{
    int i,num;
    for(i=0;i<9;i++)
    {
        e[m].temp[i]=e[k].temp[i];
    }
    e[m].temp[n]=e[m].temp[n-3];
    e[m].temp[n-3]=0;
    e[m].zero=n-3;
    for(i=0;i<9;i++)
    {
        assistion[i]=e[m].temp[i];
    }
    num=findFunction(assistion,end);
    return num;
}
int Left(int n,int k,int m)
{
    int i,num;
    for(i=0;i<9;i++)
    {
        e[m].temp[i]=e[k].temp[i];
    }
    e[m].temp[n]=e[m].temp[n+1];
    e[m].temp[n+1]=0;
    e[m].zero=n+1;
    for(i=0;i<9;i++)
    {
        assistion[i]=e[m].temp[i];
    }
    num=findFunction(assistion,end);
    return num;
}
int Right(int n,int k,int m)
{
    int i,num;
    for(i=0;i<9;i++)
    {
        e[m].temp[i]=e[k].temp[i];
    }
    e[m].temp[n]=e[m].temp[n-1];
    e[m].temp[n-1]=0;
    e[m].zero=n-1;
    for(i=0;i<9;i++)
    {
        assistion[i]=e[m].temp[i];
    }
    num=findFunction(assistion,end);
    return num;
}
void findPath(int count)
{
    bool b=false;
    int i;
    while(p>0)
    {
        int tempNum;
        if(up && !b)
        {
            e[count].father=e[count].zero+3;
            if(e[count].father!=e[count-1].father)
            {
                tempNum=Up(e[count].zero,count,count+1);
                if(tempNum<p)
                {
                    p=tempNum;b=true;
                    up=down=left=right=0;
                    findDirect(e[count+1].zero);
                }
            }
            
        }
        if(down && !b)
        {
            e[count].father=e[count].zero-3;
            if(e[count].father!=e[count-1].father)
            {
                tempNum=Down(e[count].zero,count,count+1);
                if(tempNum<p)
                {
                    p=tempNum;b=true;
                    up=down=left=right=0;
                    findDirect(e[count+1].zero);
                }
            }
            
        }
        if(left && !b)
        {
            e[count].father=e[count].zero+1;
            if(e[count].father!=e[count-1].father)
            {
                tempNum=Left(e[count].zero,count,count+1);
                if(tempNum<p)
                {
                    p=tempNum;b=true;
                    up=down=left=right=0;
                    findDirect(e[count+1].zero);
                }
            }
        }
        if(right && !b)
        {
            e[count].father=e[count].zero-1;
            if(e[count].father!=e[count-1].father)
            {
                tempNum=Right(e[count].zero,count,count+1);
                if(tempNum<p)
                {
                    p=tempNum;b=true;
                    up=down=left=right=0;
                    findDirect(e[count+1].zero);
                }
            }
        }
        count++;b=false;
    }
    for(i=0;i<=count;i++)print(i);
}
int main()
{
    int i,count=0;
    up=down=left=right=0;
    printf("请输入初态:");
    for(i=0;i<9;i++)
    {
        scanf("%d",&start[i]);
        e[count].temp[i]=start[i];
        if(start[i]==0)e[count].zero=i;
    }
    printf("请输入终态:");
    for(i=0;i<9;i++)
    {
        scanf("%d",&end[i]);
    }
    p=findFunction(start,end);
    findDirect(e[0].zero);
    findPath(count);
    return 0;
}



											
	    

	