遗传算法路径规划代码_路径规划A* 算法代码详解

f70c3e8243e55116e0977649fd7d3b3e.png

A star.h

首先放完整版代码:

#include <vector>

#include <list>

using namespace std;

const int kCost1 = 10; //直移一格消耗

const int kCost2 = 14; //斜移一格消耗

struct Point

{

int x, y; //点坐标,这里为了方便按照C++的数组来计算,x代表横排,y代表竖列

int F, G, H; //F=G+H

Point *parent; //parent的坐标,这里没有用指针,从而简化代码

/*Point(int _x, int _y) :x(_x), y(_y), F(0), G(0), H(0), parent(NULL)

{

}*/

Point(int _x, int _y)//变量初始化

{

x = _x;

y = _y;

F = 0;

G = 0;

H = 0;

parent = NULL;

}

};

class Astar

{

public:

void InitAstar(vector<vector<int>> &_maze);

list<Point *> GetPath(Point &startPoint, Point &endPoint, bool isIgnoreCorner);

private:

Point *findPath(Point &startPoint, Point &endPoint, bool isIgnoreCorner);

vector<Point *> getSurroundPoints(const Point *point, bool isIgnoreCorner) const;

bool isCanreach(const Point *point, const Point *target, bool isIgnoreCorner) const; //判断某点是否可以用于下一步判断

Point *isInList(const list<Point *> &list, const Point *point) const; //判断开启/关闭列表中是否包含某点

Point *getLeastFpoint(); //从开启列表中返回F值最小的节点

//计算FGH值

int calcG(Point *temp_start, Point *point);

int calcH(Point *point, Point *end);

int calcF(Point *point);

private:

vector<std::vector<int>> maze;

list<Point *> openList; //开启列表

list<Point *> closeList; //关闭列表

};

void Astar::InitAstar(std::vector<std::vector<int>> &_maze)

{

maze = _maze;

}

int Astar::calcG(Point *temp_start, Point *point)

{

int extraG = (abs(point->x - temp_start->x) + abs(point->y - temp_start->y)) == 1 ? kCost1 : kCost2;

int parentG = point->parent == NULL ? 0 : point->parent->G; //如果是初始节点,则其父节点是空

return parentG + extraG;

}

int Astar::calcH(Point *point, Point *end)

{

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值