`
isiqi
  • 浏览: 16077907 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

A*寻路算法的C++简单实现

阅读更多
2007/8/13 17:26:59
#include <iostream>
#include <ctime>
#include <list>
#include <algorithm>
#include <cassert>
using namespace std;
class AStarPos
{
public:
//点的x,y坐标;前一个连接点的x,y坐标;
//评估成本,起点到该点的成本
int _x, _y, _px, _py, _cost, _base_cost;
AStarPos()
{
_x = 0;
_y = 0;
_px = 0;
_py = 0;
_cost = 1000000; //应取超过实际问题中的最大成本的值
_base_cost = 1000000; //应取超过实际问题中的最大成本的值
}
AStarPos(int x, int y, int px, int py, int cost, int base_cost)
{
_x = x;
_y = y;
_px = px;
_py = py;
_cost = cost;
_base_cost = base_cost;
}
//注意声明格式,否则泛型算法没法编译通过
bool operator== (const AStarPos &p) const
{
return ((this->_x == p._x) && (this->_y == p._y));
}
//注意声明格式,否则泛型算法没法编译通过
bool operator< (AStarPos &p)
{
return this->_cost < p._cost;
}

~AStarPos()
{
}
};

int const _max = 10; //A* 寻路地图大小,此例中地图是正方形,但是也可以是矩形。
//判断当点探索点是否在指定的列表里面。
bool contains(list<AStarPos>, AStarPos&);
//寻找列表中成本最低的点
AStarPos& min_cost(list<AStarPos>);
//在列表中查找点(x,y)的信息
AStarPos& find_pos(list<AStarPos>, int x, int y);
int main()
{
int map[_max][_max], cost[_max][_max] = {0}; //地图数组
::srand((unsigned int) time(NULL));
for(int i = 0; i<_max; i++)
{
for(int j = 0; j < _max; j++)
{
if(rand() % 100 < 30){
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
int o_x = ::rand() % _max, o_y = ::rand() % _max;
int d_x = ::rand() % _max, d_y = ::rand() % _max;
stringstream ss;
for(int i = 0; i< _max; i++)
{
for(int j = 0; j < _max; j++)
{
ss<<map[i][j]<<",";
}
ss<<endl;
}
ss<<endl;
ss<<"("<<o_x<<","<<o_y<<"),("<<d_x<<","<<d_y<<")"<<endl;
cout<<ss.str()<<endl;
list<AStarPos> open, closed;
open.push_back(AStarPos(o_x, o_y, o_x, o_y, (o_x - d_x) * (o_x - d_x) + (o_y - d_y) * ( o_y - d_y), 0));
//TODO
//需要判断起点和终点是否为一个点。

int pos_x, pos_y;
while(!open.empty())
{
AStarPos curr_pos = min_cost(open);

closed.push_back(curr_pos);
open.remove(curr_pos);
//以下寻路策略认为不能斜行,可以增加45度方向的定点来实现斜形
if((curr_pos._x - 1 >= 0)){ //左侧
pos_x = curr_pos._x - 1 ;
pos_y = curr_pos._y;
//到达目标
if(pos_x == d_x && pos_y == d_y)
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1,
curr_pos._base_cost + 1);
closed.push_back(temp_pos);
break;
}
if(map[pos_x][pos_y] != 1) //不是障碍物
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1 + (pos_x - d_x) * (pos_x - d_x) + (pos_y - d_y) * ( pos_y - d_y),
curr_pos._base_cost + 1);
if(!contains(open, temp_pos) && !contains(closed, temp_pos)) //该点不在列表中
{
open.push_back(temp_pos);
cost[temp_pos._x][temp_pos._y] = temp_pos._base_cost;
}
}
}
if(curr_pos._x + 1 < _max) //右侧
{
pos_x = curr_pos._x + 1 ;
pos_y = curr_pos._y;
//到达目标
if(pos_x == d_x && pos_y == d_y)
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1,
curr_pos._base_cost + 1);
closed.push_back(temp_pos);
break;
}
if(map[pos_x][pos_y] != 1) //不是障碍物
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1 + (pos_x - d_x) * (pos_x - d_x) + (pos_y - d_y) * ( pos_y - d_y),
curr_pos._base_cost + 1);
if(!contains(open, temp_pos) && !contains(closed, temp_pos)) //该点不在列表中
{
open.push_back(temp_pos);
cost[temp_pos._x][temp_pos._y] = temp_pos._base_cost;
}
}
}
if(curr_pos._y - 1 >= 0) //上侧
{
pos_x = curr_pos._x ;
pos_y = curr_pos._y - 1;
//到达目标
if(pos_x == d_x && pos_y == d_y)
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1,
curr_pos._base_cost + 1);
closed.push_back(temp_pos);
break;
}
if(map[pos_x][pos_y] != 1) //不是障碍物
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1 + (pos_x - d_x) * (pos_x - d_x) + (pos_y - d_y) * ( pos_y - d_y),
curr_pos._base_cost + 1);
if(!contains(open, temp_pos) && !contains(closed, temp_pos)) //该点不在列表中
{
open.push_back(temp_pos);
cost[temp_pos._x][temp_pos._y] = temp_pos._base_cost;
}
}
}
if(curr_pos._y + 1 < _max) //下侧
{
pos_x = curr_pos._x ;
pos_y = curr_pos._y + 1;
//到达目标
if(pos_x == d_x && pos_y == d_y)
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1,
curr_pos._base_cost + 1);
closed.push_back(temp_pos);
break;
}
if(map[pos_x][pos_y] != 1) //不是障碍物
{
AStarPos temp_pos(pos_x,pos_y, curr_pos._x, curr_pos._y,
curr_pos._base_cost + 1 + (pos_x - d_x) * (pos_x - d_x) + (pos_y - d_y) * ( pos_y - d_y),
curr_pos._base_cost + 1);
if(!contains(open, temp_pos) && !contains(closed, temp_pos)) //该点不在列表中
{
open.push_back(temp_pos);
cost[temp_pos._x][temp_pos._y] = temp_pos._base_cost;
}
}
}
//TODO 增加斜边的四个顶点的测试,来实现斜形效果。
}
if(pos_x == d_x && pos_y == d_y)
{
while((pos_x != o_x) || (pos_y != o_y))
{

cout<<"("<<pos_x<<", "<<pos_y<<")<-";
map[pos_x][pos_y] = 100;
AStarPos temp = find_pos(closed, pos_x, pos_y);
pos_x = temp._px;
pos_y = temp._py;
}
cout<<"("<<pos_x<<", "<<pos_y<<")"<<endl;
}
else
{
cout<<"寻路失败!"<<endl;
}
stringstream ss_o, ss_cost;
for(int i = 0; i < _max; i++)
{
for(int j = 0; j < _max; j++)
{
if(((o_x == i) && (o_y == j)) ||((d_x == i) && (d_y == j)))
{
ss_o<<"+"<<",";
}
else if((map[i][j] == 100))
{
ss_o<<"="<<",";
}
else
{
ss_o<<map[i][j]<<",";
}
ss_cost<<cost[i][j]<<",";
}
ss_o<<endl;
ss_cost<<endl;
}
ss_o<<endl;
ss_cost<<endl;
cout<<"寻路结果:"<<endl;
cout<<ss_o.str()<<endl;
cout<<"成本评估:"<<endl;
cout<<ss_cost.str()<<endl;
system("pause");
}
bool contains(list<AStarPos> a, AStarPos& b)
{
list<AStarPos>::iterator ind;
for(ind = a.begin(); ind !=a.end(); ind++)
{
if(((*ind)._x == b._x) && ((*ind)._y ==b._y))
{
return true;
}
}
return false;
}
AStarPos& min_cost(list<AStarPos> a)
{
int cost = 1000000;
AStarPos res;
list<AStarPos>::iterator ind;
for(ind = a.begin(); ind !=a.end(); ind++)
{
if((*ind)._cost < cost)
{
cost = (*ind)._cost;
res = *ind;
}
}
return AStarPos(res._x, res._y, res._px, res._py, res._cost, res._base_cost);
}

AStarPos& find_pos(list<AStarPos> a, int x, int y)
{
AStarPos res;
list<AStarPos>::iterator ind;
for(ind = a.begin(); ind !=a.end(); ind++)
{
if((*ind)._x == x && (*ind)._y == y)
{
return AStarPos((*ind)._x, (*ind)._y, (*ind)._px, (*ind)._py, (*ind)._cost, (*ind)._base_cost);
}
}

return AStarPos(res._x, res._y, res._px, res._py, res._cost, res._base_cost);

分享到:
评论

相关推荐

    a*寻路 c++实现

    a*寻路算法, 八方向 ,c++ 二叉堆 实现,vs2010下可直接编译运行 可用于游戏寻路等 注:这是某开源代码的修正版,原版有错误和无意义代码,不能发就算了

    A*算法、自动寻路算法C++源码

    A*算法的C++实现,注释详尽,直接编译运行

    a*寻路算法,c++类

    纯C++算法类,头文件有注释,调用简单,速度极快

    A*寻路算法(C++)

    用A*算法实现的走迷宫,附C++源码和相应资料,你可以随意修改和使用这些代码。

    A*算法 C++实现

    A*算法都知道吧。经典算法。大量用在游戏中的自动寻路中

    A星寻路算法&B星寻路算法 c++实现 MFC

    代码中实现了3种寻路算法AStar,AStar_Direct,BStar() 在VS2019环境下运行,建议以release方式运行,DEBUG没有调会崩溃

    A星寻路算法c++语言实现

    A*算法,A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法。算法中的距离估算值与实际值越接近,最终搜索速度越快。c++语言实现

    A*算法的c++实现+opencv动态显示

    使用vs2015和c++实现A*算法,并使用opencv动态显示A*算法的寻路过程及结果,包含动态视频

    A*算法(Astar算法/A星算法)C++模板函数实现.zip

    这是A*算法的C++(MSVC)实现,利用了模板函数,有两个测试用例,一个是迷宫寻路,一个是求解八数码问题。 压缩包内包括: Astar.hpp //这是A*算法的模板函数实现 (还附送个快排算法 testMain_eightDigital.cpp //这...

    A星算法自动寻路(C++源代码)

    A星算法 游戏中自动寻路的实现(C++源代码)

    C++实现的高效A*算法

    这是使用C++实现的高效的A-Star算法。只对算法的程序实现做了尽力而为的优化,并没有对算法自身进行改良。优化措施主要在于:快速判断路径节点是否在开启/关闭列表中、快速查找最小f值的节点以及优化路径节点频繁...

    迷宫问题A*算法

    本科生计算机相关专业 人工智能课程 A*算法解决迷宫问题C++代码 详细注释,易懂

    人工智能 A*算法实现自动寻路的程序

    本程序利用A*算法模拟了机器人自动寻路的过程,程序中可以通过点击地图来设置/取消障碍位置,实现不同情况下的路径寻找。当然你也可以修改寻路的起点和终点以及地图的大小~

    A星算法C++实现

    C++实现的A*寻路算法,经过测试,在有障碍物的情况下,路径为期望路径,内附测试结果,可以修改地图的大小及障碍物位置,比如大小改为1920*1080,使其更接近真实电脑屏幕或者手机屏幕分辨率,得到更为贴近实际的运算...

    广度优先算法、最佳优先算法、A*算法寻路程序

    一个用广度优先算法、最佳优先算法、A*算法寻路的程序,版本VS2015,用c++编写,mfc可视化,用动画将每种算法的搜索过程展现出来。

    vc游戏编程库的源程序,如A*算法 A星算法 AStar自动寻路算法

    visual c++游戏编程库的源程序,如A*算法 A星算法 AStar自动寻路算法

    C语言实现:使用A*算法来解决15数码问题

    本报告利用A*算法,给出了15数码问题的C++算法实现。 A*算法是一种预测算法,主要用于寻路等,根据当前状态和目标状态之间的差异,预测到达目标需要多少开销,根据这个开销来判断下一次选择以那个状态开始。这个开销...

    C++A算法最短寻路算法

    A星C++A行算法 寻路算法 代码很少 很经典

    A*算法源码

    A星寻路算法(A*算法)源码实现,用MFC程序模拟动态寻路过程。只实现了最简单的A*算法,MFC消息处理的也不好,仅作参考。

Global site tag (gtag.js) - Google Analytics