A*寻路算法伪代码

                                                               A*寻路算法伪代码

     这是今天读了GameDev上的一篇关于A*算法的文章自己总结的一段伪代码,基本上描述清楚了A*算法的大致思路.

heap.add(firstnode)      //将开始节点加入堆,同时也是加入openlist
do
{
 fleast=heap.pop();            //从openlist取fcost最小的元素
 current=fleast;                   //取出的元素作为当前节点
 fleast.flag=1 //1:on close 0:not on open nor on close
 for(int i=current.x-1;i<current.x+1;i++)     //考察当前节点的所有相邻节点
 for(int j=current.y-1;j<current.y+1;j++)
 {
  if(map[i][j]!=unwalkable||!onclose(node(i,j)))         //相邻节点可通并且没有考察过
  {
  if(!onopen(node(i,j)))                                    //相邻节点不在openlist中
  {
  heap.add(node(i,j));                                    //加入openlist
  node(i,j).parent=current;                            //设置相邻节点的父节点为当前节点
  node(i,j).calculate(f,g,h);                           //重新计算fcost,gcost,hcost
  
  if(node(i,j)==dest)                                     //如果加入的节点是目标点则找到路径
  path=find;
  }
  else
  {
  temp.gcost=parent.gcost+addcost;         //相邻节点已经在openlist中
  node(i,j).hcost=10*(abs(i-parent.x)+abs(j-parent.y));
  node(i,j).fcost=node(i,j).gcost+node(i,j).hcost;
  if(tempgcost<node(i,j).gcost)
  node(i,j).parent=current;
  node(i,j).recaculate(f,g,h);                             //重新计算fcost,gcost,hcost
  
 }
 }
 
}while(!hp.isempty())                                  //如果堆空则没有找到路径
if(path==find)
outpath;                                                      //输出路径
else
cout<<"path not find;!"<<endl;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值