A*算法详解(转)
来源:互联网 发布:如何在淘宝上注册网店 编辑:程序博客网 时间:2024/05/21 21:49
A*算法详解(转)
第一部分:A*算法简介一、何谓启发式搜索算法:
二、初识A*算法:
第二部分:深入A*算法———浅析A*算法在搜索最短路径中的应用
一、前言
二、A*算法的程序编写原理
Best_First_Search(){Open = [起始节点]; Closed = [];while ( Open表非空 ){从Open中取得一个节点X,并从OPEN表中删除。if (X是目标节点){求得路径PATH;返回路径PATH;}for (每一个X的子节点Y){if( Y不在OPEN表和CLOSE表中 ){求Y的估价值;并将Y插入OPEN表中;//还没有排序}elseif( Y在OPEN表中 ){if( Y的估价值小于OPEN表的估价值 )更新OPEN表中的估价值;}else //Y在CLOSE表中{if( Y的估价值小于CLOSE表的估价值 ){更新CLOSE表中的估价值;从CLOSE表中移出节点,并放入OPEN表中;}}将X节点插入CLOSE表中;按照估价值将OPEN表中的节点排序;}//end for}//end while}//end func
啊!伪程序出来了,写一个源程序应该不是问题了,依葫芦画瓢就可以。A*算法的程序与此
三、用A*算法实现最短路径的搜索
先看搜索主函数:
void AstarPathfinder::FindPath(int sx, int sy, int dx, int dy){NODE *Node, *BestNode;int TileNumDest;//得到目标位置,作判断用TileNumDest = TileNum(sx, sy);//生成Open和Closed表OPEN=( NODE* )calloc(1,sizeof( NODE ));CLOSED=( NODE* )calloc(1,sizeof( NODE ));//生成起始节点,并放入Open表中Node=( NODE* )calloc(1,sizeof( NODE ));Node->g = 0;//这是计算h值Node->h = (dx-sx)*(dx-sx) + (dy-sy)*(dy-sy); // should really use sqrt().//这是计算f值,即估价值Node->f = Node->g+Node->h;Node->NodeNum = TileNum(dx, dy);Node->x = dx;Node->y = dy;OPEN->NextNode=Node; // make Open List point to first nodefor (;;){ //从Open表中取得一个估价值最好的节点BestNode=ReturnBestNode();//如果该节点是目标节点就退出if (BestNode->NodeNum == TileNumDest) // if we’ve found the end, break and finishbreak;//否则生成子节点GenerateSuccessors(BestNode,sx,sy);}PATH = BestNode;}再看看生成子节点函数 GenerateSuccessors: void AstarPathfinder::GenerateSuccessors(NODE *BestNode, int dx, int dy){int x, y;//哦!依次生成八个方向的子节点,简单!// Upper-Leftif ( FreeTile(x=BestNode->x-TILESIZE, y=BestNode->y-TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Upperif ( FreeTile(x=BestNode->x, y=BestNode->y-TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Upper-Rightif ( FreeTile(x=BestNode->x+TILESIZE, y=BestNode->y-TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Rightif ( FreeTile(x=BestNode->x+TILESIZE, y=BestNode->y) )GenerateSucc(BestNode,x,y,dx,dy);// Lower-Rightif ( FreeTile(x=BestNode->x+TILESIZE, y=BestNode->y+TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Lowerif ( FreeTile(x=BestNode->x, y=BestNode->y+TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Lower-Leftif ( FreeTile(x=BestNode->x-TILESIZE, y=BestNode->y+TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);// Leftif ( FreeTile(x=BestNode->x-TILESIZE, y=BestNode->y) )GenerateSucc(BestNode,x,y,dx,dy);}看看最重要的函数GenerateSucc: void AstarPathfinder::GenerateSucc(NODE *BestNode,int x, int y, int dx, int dy){int g, TileNumS, c = 0;NODE *Old, *Successor;//计算子节点的 g 值g = BestNode->g+1; // g(Successor)=g(BestNode)+cost of getting from BestNode to SuccessorTileNumS = TileNum(x,y); // identification purposes//子节点再Open表中吗?if ( (Old=CheckOPEN(TileNumS)) != NULL ) // if equal to NULL then not in OPEN list, else it returns the Node in Old{//若在for( c = 0; c <8; c++) if( BestNode->Child[c] == NULL ) // Add Old to the list of BestNode’s Children (or Successors).break;BestNode->Child[c] = Old;//比较Open表中的估价值和当前的估价值(只要比较g值就可以了)if ( g g ) // if our new g value is Parent = BestNode;Old->g = g;Old->f = g + Old->h;}}else //在Closed表中吗?if ( (Old=CheckCLOSED(TileNumS)) != NULL ) // if equal to NULL then not in OPEN list, else it returns the Node in Old{//若在for( c = 0; c<8; c++) if ( BestNode->Child[c] == NULL ) // Add Old to the list of BestNode’s Children (or Successors).break;BestNode->Child[c] = Old;//比较Closed表中的估价值和当前的估价值(只要比较g值就可以了)if ( g g ) // if our new g value is Parent = BestNode;Old->g = g;Old->f = g + Old->h;//再依次更新Old的所有子节点的估价值PropagateDown(Old); // Since we changed the g value of Old, we need// to propagate this new value downwards, i.e.// do a Depth-First traversal of the tree!}}else//不在Open表中也不在Close表中{//生成新的节点Successor = ( NODE* )calloc(1,sizeof( NODE ));Successor->Parent = BestNode;Successor->g = g;Successor->h = (x-dx)*(x-dx) + (y-dy)*(y-dy); // should do sqrt(), but since we don’t reallySuccessor->f = g+Successor->h; // care about the distance but just which branch looksSuccessor->x = x; // better this should suffice. Anyayz it’s faster.Successor->y = y;Successor->NodeNum = TileNumS;//再插入Open表中,同时排序。Insert(Successor); // Insert Successor on OPEN list wrt ffor( c =0; c <8; c++) if ( BestNode->Child[c] == NULL ) // Add Old to the list of BestNode’s Children (or Successors).break;BestNode->Child[c] = Successor;}}
哈哈!A*算法我懂了!当然,我希望你有这样的感觉!不过我还要再说几句。仔细看看这个程
续:
经典算法研究系列:一、A*搜索算法 阅读全文
0 0
- A算法详解[转]
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解(转)
- A*算法详解
- A*算法详解
- A* 算法详解
- A*算法详解
- A*算法详解
- A*算法详解
- A*算法详解
- A*算法详解
- A*算法详解
- A*算法详解
- 使用while循环语句计算1+1/2!+1/3!+···+1/20!之和
- MYSQL 操作数据库
- Eclipse运行性能调优
- Matplotlib 对 LaTeX 的支持
- Unity常用的设计模式_工厂模式系列之工厂方法模式
- A*算法详解(转)
- Android之Mediaplayer音乐播放器
- 欢迎使用CSDN-markdown编辑器
- 第 3 章 线性表
- Struts2中的#、$、%的区别
- bzoj 3218: a + b Problem 最小割+可持久化线段树
- 欲学习的内容
- 暴力大神hxx 树形dp
- java Rserve测试