资源简介
使用AStar算法实现了一个简单的demo,亲测可用,代码不多流程简单,一看就会
![](http://www.nz998.com/pic/57484.jpg)
代码片段和文件信息
#include “astar.h“
#include
#include
AStar::AStar(int row int col)
{
m_col = col;
m_row = row;
}
AStar::~AStar()
{
openList.clear();
closeList.clear();
obstacleList.clear();
}
void AStar::lookPath(NodeItem &start NodeItem &goal)
{
this->goal = goal;
openList.clear();
closeList.clear();
//把起点插入到开启列表本函数插入元素按照元素的f(x)值进行升序排序保证第一个元素的f(x)值最小
addToOpenList(start);
list::iterator it;
while (openList.size()) {
it = openList.begin();
//取出f(x)值最小的元素
NodeItem first = *it;
qDebug()<<“node X:“< if(*it == goal)
{
qDebug()<<(*it).parent->x<<(*it).parent->y;
addPathNodeListByGoal(*it);
qDebug()<<“finished=============addPathNodeListByGoal“;
break;
}
openList.pop_front();
//把这个元素周围能到达的点添加进开启列表添加后按照元素的f(x)值的大小进行排序(建议使用二分法把当前元素插入开启列表)
closeList.push_back(*it);
addNeighBorNode(first);
}
printPathNodeList();
}
void AStar::addNode(const NodeItem ¢er int direct)
{
}
void AStar::addNeighBorNode(const NodeItem ¢er)
{
qDebug()<<“Center x: “< for(int i=0; i<8; i++)
{
if((center.x+dir[i][0])<0 || (center.x+dir[i][0])>=m_col || (center.y+dir[i][1])<0 || (center.y+dir[i][1])>=m_row)
{
qDebug()<<“hehehehehehehheehe“< continue;
}else
{
NodeItem item;
item.x = center.x+dir[i][0];
item.y = center.y+dir[i][1];
if(i%2==0)
{
item.g = center.g+10;
}else
{
item.g = center.g+14;
}
int xGap = abs(goal.x-item.x);
int yGap = abs(goal.y-item.y);
int minValue = xGap>yGap? yGap : xGap;
item.h = abs(xGap-yGap)*10+minValue*14;
item.f = item.fx();
item.parent = new NodeItem;
item.parent->x = center.x;
item.parent->y = center.y;
item.parent->g = center.g;
item.parent->h = center.h;
item.parent->f = center.f;
if(isOnCloseList(item))
{
continue;
}
if(!isOnOpenList(item))
{
addToOpenList(item);
}else
{
list::iterator it;
for(it = openList.begin(); it != openList.end(); it++)
{
if(item == (*it))
{
if(center.g+(i%2==0? 10:14)<(*it).g)
{
addToOpenList(item);
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-11-29 16:23 AStar\
文件 1101 2018-08-13 17:45 AStar\AStar.pro
文件 45687 2018-11-29 16:23 AStar\AStar.pro.user
文件 5862 2018-08-22 10:43 AStar\astar.cpp
文件 1898 2018-08-18 14:02 AStar\astar.h
文件 177 2018-08-16 15:19 AStar\main.cpp
文件 1798 2018-08-17 17:15 AStar\rect.cpp
文件 761 2018-08-17 17:15 AStar\rect.h
文件 1778 2018-08-18 14:02 AStar\widget.cpp
文件 533 2018-08-16 15:39 AStar\widget.h
文件 441 2018-08-13 17:42 AStar\widget.ui
- 上一篇:ABC带约束优化算法
- 下一篇:微信小程序-教务系统源代码
相关资源
- VisualStudioUninstaller vs卸载工具
- 组态王驱动开发包3.0.0.7(中文)
- 多窗口后台鼠标连点器
- 使用选择性重传协议实现UDP可靠通信
- VC 获得文件属性 获取文件的创建时
- 读者写者问题(读者优先,写者优先
- 用VC 编写的仿QQ聊天室程序源代码
- 外点法程序
- 外罚函数程序
- qt-电子点菜系统
- 推箱子及人工智能寻路C 源代码
- 自己写的航空订票系统c 版--数据结构
- 数据结构实验魔王语言
- MUSIC算法c 实现
- C 餐厅叫号系统(QT平)
- 国际象棋c 完整版
-
ob
jectARX给Auto CAD加工具条 - 画图程序MFC/VC/VC CRectTracker 串行化
- MFC网络编程实例
- c 课程设计 职工信息管理系统
- VC 游戏编程—附源代码
- IpHlpApi.h&IpHlpApi.lib
- 清华大学 c 郑莉 ppt课件
- c 程序判断离散数学中命题公式
- 多项式求和(数据结构C 版)
- vc 6.0开发的流程图编辑器
- VC 天空盒(skyBox)实现(附源代码)
- c MFC 画多边形
- 用C 实现的对网络上的ARP数据包进行
- Microsoft基本类库 (MFC)(C 库)
评论
共有 条评论