最近舍友突然说起a*算法 虽然之前看过 但是我发现自己记得不是很清楚了 而且从来没去手动实现过 趁着这次就实现一下加深理解 去网上查了下原理 看了几篇别人的实现 然后按自己理解综合一下写出来记录一下(我参考那篇程序运行有好几个问题,而且运行得出路径也是不对的,不知道他有没跑过的。)
Astar.h
#ifndef ASTAR_H
#define ASTAR_H
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include<algorithm>
using namespace std;
typedef struct Node
{
int x,y;
int g; //起始点到当前点实际代价
int h;//当前节点到目标节点最佳路径的估计代价
int f;//估计值
Node* father;
Node(int x,int y)
{
this->x = x;
this->y = y ;
this->g = 0;
this->h = 0;
this->f = 0;
this->father = NULL;
}
Node(int x,int y,Node* father)
{
this->x = x;
this->y = y ;
this->g = 0;
this->h = 0;
this->f = 0;
this->father = father;
}
}Node;
class Astar{
public:
Astar();
~Astar();
void search(Node* startPos,Node* endPos);
void checkPoit(int x,int y,Node* father,int g);
void NextStep(Node* currentPoint);
int isContains(vector<Node*>* Nodelist ,int x,int y);
void

本文记录了作者实现A*算法的过程,通过查阅资料和理解原理,结合他人代码进行综合实践,并指出了参考代码存在的问题。作者提供了一份可以运行的C++代码,包括Astar.h、Astar.cpp和main.h文件,经验证得出的路径正确。但作者也注意到,对于大型地图,由于容器频繁删除导致效率问题,建议使用辅助栈和排序优化来避免容器大小的重新分配,提高算法效率。
最低0.47元/天 解锁文章
1848





