【轉(zhuǎn)】優(yōu)化后的A*算法
優(yōu)化后的A*算法--簡潔、易懂且實(shí)用
添加時間:2006-12-15 出處:互聯(lián)網(wǎng) 作者:zhangshuo
我曾看過一些有關(guān)A*算法的程序,不過寫得比較簡潔、易懂的還是風(fēng)云寫的A*算法教學(xué)實(shí)例(風(fēng)云工作室),但是這個算法并沒有進(jìn)行優(yōu)化,該程序要用到實(shí)際應(yīng)用中,還會有一定的限制,所以我對該算法進(jìn)行了改進(jìn),并加上更詳細(xì)的算法說明,使其具有更好的教學(xué)作用和實(shí)用價值。開始前我先給出A*算法的基本思路:
問題:求出2D的迷宮中起始點(diǎn)S到目標(biāo)點(diǎn)E的最短路徑?
算法: findpath()
{
把S點(diǎn)加入樹根(各點(diǎn)所在的樹的高度表示從S點(diǎn)到該點(diǎn)所走過的步數(shù));
把S點(diǎn)加入排序隊列(按該點(diǎn)到E點(diǎn)的距離排序+走過的步數(shù)從小到大排序);
1、排序隊列sort_queue中距離最小的第一個點(diǎn)出列,并保存入store_queue中
2、從出列的點(diǎn)出發(fā),分別向4個(或8個)方向中的一個各走出一步
3、并估算第2步所走到位置到目標(biāo)點(diǎn)的距離,并把該位置加入樹,
最后把該點(diǎn)按距離從小到大排序后并放入隊列中。(由trytile函數(shù)實(shí)現(xiàn))。
4、如果該點(diǎn)從四個方向上都不能移動,則把該點(diǎn)從store_queue中刪除
5、回到第一點(diǎn),直到找到E點(diǎn)則結(jié)束
從目標(biāo)點(diǎn)回溯樹,直到樹根則可以找到最佳路徑,并保存在path[]中
}
/*========================================================================
精簡的A*算法 作者:添翼虎
網(wǎng)址:http://tyhweb.163.net Email:tyhweb@163.net
本程序參考了風(fēng)云的最短路徑代碼(http://member.nease.com/~cloudwu),
并加以改進(jìn)和優(yōu)化:
1、把原來用于存放已處理節(jié)點(diǎn)的堆棧改為(store_queue)隊列,這樣在從
sort_queue隊列出列時可直接放入store_queue中。
2、解除了地圖大小的限制(如果有64K內(nèi)存限制時,地圖大小只能是180x180)
3、刪除了原程序中的一些冗余,見程序中的注釋。
4、程序繼續(xù)使用dis_map數(shù)組保存各點(diǎn)歷史歷史最佳距離,也包含了某點(diǎn)是否已經(jīng)
經(jīng)過的信息,雖然這樣做可能會比使用鏈表多用一些內(nèi)存,但是在搜索時可以
節(jié)省不時間。
5、程序更具有實(shí)用性,可直接或修改后運(yùn)用于你的程序中,但請你使用該代碼后
應(yīng)該返回一些信息給我,如算法的改進(jìn)或使用于什么程序等。
本程序可以用Borland C++或DJGPP編譯,并附帶有一個數(shù)據(jù)文件 map.dat,
保存有地圖的數(shù)據(jù),(注:該地圖文件格式與風(fēng)云的原代碼的地圖格式不一樣)
-------------------------------------------------------------------------*/
//#define NDEBUG
#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include <stdlib.h>
#define tile_num(x,y) ((y)*map_w+(x)) //將 x,y 坐標(biāo)轉(zhuǎn)換為地圖上塊的編號
#define tile_x(n) ((n)%map_w) //由塊編號得出 x,y 坐標(biāo)
#define tile_y(n) ((n)/map_w)
#define MAPMAXSIZE 180 //地圖面積最大為 180x180,如果沒有64K內(nèi)存限制可以更大
#define MAXINT 32767
//樹結(jié)構(gòu), 比較特殊, 是從葉節(jié)點(diǎn)向根節(jié)點(diǎn)反向鏈接,方便從葉節(jié)點(diǎn)找到根節(jié)點(diǎn)
typedef struct tree_node *TREE;
struct tree_node {
int h; //節(jié)點(diǎn)所在的高度,表示從起始點(diǎn)到該節(jié)點(diǎn)所有的步數(shù)
int tile; //該節(jié)點(diǎn)的位置
TREE father; //該節(jié)點(diǎn)的上一步
};
//鏈接結(jié)構(gòu),用于保存處理過的和沒有處理過的結(jié)點(diǎn)
typedef struct link_node *LINK;
struct link_node {
TREE node;
int f;
LINK next;
};
LINK sort_queue; // 保存沒有處理的行走方法的節(jié)點(diǎn)
LINK store_queue; // 保存已經(jīng)處理過的節(jié)點(diǎn) (搜索完后釋放)
unsigned char * map; //地圖數(shù)據(jù)
unsigned int * dis_map; //保存搜索路徑時,中間目標(biāo)地最優(yōu)解
int map_w,map_h; //地圖寬和高
int start_x,start_y,end_x,end_y; //地點(diǎn),終點(diǎn)坐標(biāo)
// 初始化隊列
void init_queue()
{
sort_queue=(LINK)malloc(sizeof(*sort_queue));
sort_queue->node=NULL;
sort_queue->f=-1;
sort_queue->next=(LINK)malloc(sizeof(*sort_queue));
sort_queue->next->node=NULL;
sort_queue->next->f=MAXINT;
sort_queue->next->next=NULL;
store_queue=(LINK)malloc(sizeof(*store_queue));
store_queue->node=NULL;
store_queue->f=-1;
store_queue->next=NULL;
}
// 待處理節(jié)點(diǎn)入隊列, 依靠對目的地估價距離插入排序
void enter_queue(TREE node,int f)
{
LINK p=sort_queue,father,q;
while(f>p->f) {
father=p;
p=p->next;
assert(p);
}
q=(LINK)malloc(sizeof(*q));
assert(sort_queue);
q->f=f,q->node=node,q->next=p;
father->next=q;
}
// 將離目的地估計最近的方案出隊列
TREE get_from_queue()
{
LINK bestchoice=sort_queue->next;
LINK next=sort_queue->next->next;
sort_queue->next=next;
bestchoice->next=store_queue->next;
store_queue->next=bestchoice;
return bestchoice->node;
}
// 釋放棧頂節(jié)點(diǎn)
void pop_stack()
{
LINK s=store_queue->next;
assert(s);
store_queue->next=store_queue->next->next;
free(s->node);
free(s);
}
// 釋放申請過的所有節(jié)點(diǎn)
void freetree()
{
int i;
LINK p;
while(store_queue){
p=store_queue;
free(p->node);
store_queue=store_queue->next;
free(p);
}
while (sort_queue) {
p=sort_queue;
free(p->node);
sort_queue=sort_queue->next;
free(p);
}
}
// 估價函數(shù),估價 x,y 到目的地的距離,估計值必須保證比實(shí)際值小
int judge(int x,int y)
{
int distance;
distance=abs(end_x-x)+abs(end_y-y);
return distance;
}
// 嘗試下一步移動到 x,y 可行否
int trytile(int x,int y,TREE father)
{
TREE p=father;
int h;
if (map[tile_num(x,y)]!=' ') return 1; // 如果 (x,y) 處是障礙,失敗
//這一步用來判斷(x,y)點(diǎn)是否已經(jīng)加入隊列,多余可以刪除,因為dis_map已經(jīng)
//保存該點(diǎn)是否已經(jīng)保存
//while (p) {
// if (x==tile_x(p->tile) && y==tile_y(p->tile)) return 1; //如果 (x,y) 曾經(jīng)經(jīng)過,失敗
// p=p->father;
//}
h=father->h+1;
if (h>=dis_map[tile_num(x,y)]) return 1; // 如果曾經(jīng)有更好的方案移動到 (x,y) 失敗
dis_map[tile_num(x,y)]=h; // 記錄這次到 (x,y) 的距離為歷史最佳距離
// 將這步方案記入待處理隊列
p=(TREE)malloc(sizeof(*p));
p->father=father;
p->h=father->h+1;
p->tile=tile_num(x,y);
enter_queue(p,p->h+judge(x,y));
return 0;
}
// 路徑尋找主函數(shù)
int * findpath(void)
{
TREE root;
int i,j;
int * path;
memset(dis_map,0xff,map_h*map_w*sizeof(*dis_map)); //填充dis_map為0XFF,表示各點(diǎn)未曾經(jīng)過
init_queue();
root=(TREE)malloc(sizeof(*root));
root->tile=tile_num(start_x,start_y);
root->h=0;
root->father=NULL;
enter_queue(root,judge(start_x,start_y));
for (;;) {
int x,y,child;
TREE p;
root=get_from_queue();
if (root==NULL) {
return NULL;
}
x=tile_x(root->tile);
y=tile_y(root->tile);
if (x==end_x && y==end_y) break; // 達(dá)到目的地成功返回
child=trytile(x,y-1,root); //嘗試向上移動
child&=trytile(x,y+1,root); //嘗試向下移動
child&=trytile(x-1,y,root); //嘗試向左移動
child&=trytile(x+1,y,root); //嘗試向右移動
//child&=trytile(x+1,y-1,root);//嘗試向右上移動
//child&=trytile(x+1,y+1,root); //嘗試向右下移動
//child&=trytile(x-1,y+1,root); //嘗試向左下移動
//child&=trytile(x-1,y-1,root); //嘗試向左上移動
if (child!=0)
pop_stack(); // 如果四個方向均不能移動,釋放這個死節(jié)點(diǎn)
}
// 回溯樹,將求出的最佳路徑保存在 path[] 中
path=(int*)malloc((root->h+2)*sizeof(int));
assert(path);
for (i=0;root;i++) {
path[i]=root->tile;
root=root->father;
}
path[i]=-1;
freetree();
return path;
}
void printpath(int *path)
{
int i;
if(path==NULL) return ;
for (i=0;path[i]>=0;i++) {
gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);
cprintf(".");
}
}
int readmap()
{
FILE *f;
int i,j;
f=fopen("map.dat","r");
assert(f);
fscanf(f,"%d,%d\n",&map_w,&map_h);
map=malloc(map_w*map_h+1);
assert(map);
for(i=0;i<map_h;i++)
posted on 2007-07-22 15:42 Tim 閱讀(1112) 評論(0) 編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)和算法