| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 614 人关注过本帖
标题:[讨论]A*算法的解法
只看楼主 加入收藏
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
收藏
 问题点数:0 回复次数:0 
[讨论]A*算法的解法

自己这两天看算法书,写的一个关于八数码的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;
}

搜索更多相关主题的帖子: 解法 算法 
2007-03-31 23:33
快速回复:[讨论]A*算法的解法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019184 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved