A星算法-自动寻路-c++

来源:互联网 发布:数据标准化的几种方法 编辑:程序博客网 时间:2024/06/05 06:49
一共分三个文件,一个头文件两个CPP文件


 


A星原理可以去网上搜。


 


那我就直接上代码啦!


 


A星的头文件


 


[cpp] view plaincopy
/************************************************************************/  
/* author: 小鲁 
/* version: 1.0 
/* update: 06/26/10 
/************************************************************************/  
#include "stdafx.h"  
#include <list>  
#include <math.h>  
#include "iostream"  
#include <assert.h>  
using namespace std;  
// 坐标点  
typedef struct  _point  
{  
    int x;  
    int y;  
    int isblock;  
    _point()  
    {  
        x = 0;  
        y = 0;  
    }  
    _point(int _x, int _y)  
    {  
        x = _x;  
        y = _y;  
    }  
}point;  
//结点  
typedef struct _Node  
{  
    point pt;  
    int f, g, h;  
    struct _Node *preNode;//父节点  
}Node;  
/************************************************************************/  
/* A 星算法                                                                     */  
/************************************************************************/  
class AStar{  
public:  
    AStar(void);  
    ~AStar(void);  
public:  
    static const int SPEND_H = 10;//H值耗费  
    static const int SPEND_G_FAR = 14;//远的G值耗费  
    static const int SPEND_G_NEAR = 10;//近的G值耗费  
    list<Node*> psOpenList;   //开放列表  
    list<Node*> psCloseList;//封闭列表  
    Node *sStartNode;   //起始节点  
    Node *sEndNode; //终止节点  
    Node *pcurrNode;//是否是终点? NOT NULL : NULL  
public:  
    void SetStartNode(point st){sStartNode->pt.x = st.x;sStartNode->pt.y = st.y;};//设定起始节点  
    void SetEndNode(point et){sEndNode->pt.x = et.x;sEndNode->pt.y = et.y;};//设定结束节点  
    void Search();//主搜索  
    void AddToOpenList(Node *src);//添加到开放列表  
    void GetNodeByPoint(Node *pareNode, int _x, int _y);//通过坐标添加到开放列表  
    bool IsClosed(Node *src);//坐标点是否在封闭列表中  
    bool IsInOpenList(Node *src);//坐标点是否在开放列表中  
    bool IsBlock(Node *src);//坐标点是否是阻挡点  
    void SetH(Node *node);//设定H的耗费  
    void SetG(Node *node);//设定G的耗费  
    void SetF(Node *node);//设定F的耗费  
    Node* GetBestNode(); //得到最优的节点(F值最低的节点)  
    bool isPath(list<point> *path, int i, int j);//测试方法-用于输出  
};  
 


A星的实现文件


 


[cpp] view plaincopy
/************************************************************************/  
/* author: 小鲁 
/* version: 1.0 
/* update: 06/26/10 
/************************************************************************/  
#include "stdafx.h"  
#include "AStar.h"  
AStar::AStar(void)  
{  
    sStartNode = new Node();  
    sEndNode = new Node();  
    pcurrNode = NULL;  
}  
AStar::~AStar(void)  
{  
    delete sStartNode;  
    delete sEndNode;  
    delete pcurrNode;  
}  
void AStar::Search()  
{  
    Node *currentNode;//当前节点  
    currentNode = sStartNode;  
    currentNode->preNode = NULL;//初始节点的父节点为空  
    currentNode->g = 0;  
    SetH(currentNode);  
    SetF(currentNode);  
    psOpenList.push_front(sStartNode);//添加起始点到开放列表  
    do   
    {  
        currentNode = GetBestNode();//从开放列表得到最优节点  
        //遍历当前结点周围的结点并加入开放列表,当前节点从开放列表中移到封闭列表中  
        AddToOpenList(currentNode);  
          
        psCloseList.push_front(currentNode);//添加到关闭列表中  
        if(psOpenList.size() < 1 || pcurrNode)break; //如果目标节点已经存在于开放列表或开放列表是空则退出  
    } while (true);  
//*******************************测试方法Start*******************************//  
    list<point> path; //用于存放寻路后的数据  
    do   
    {  
        path.push_front(currentNode->pt);  
        cout<<currentNode->pt.x<<"/t"<<currentNode->pt.y<<endl;  
        if(currentNode->pt.x == sStartNode->pt.x && currentNode->pt.y == sStartNode->pt.y)break;  
        currentNode = currentNode->preNode;  
          
    } while (true);  
    //输出模拟的地图和寻路后的路径  
    for(int j=0;j<10;++j)  
    {  
        for (int i=0;i<10;++i)  
        {  
            if(isPath(&path,i,j))  
                cout<<"〇"<<"/t";  
            else  
                cout<<"×"<<"/t";  
        }  
        cout<<endl;  
    }  
//*******************************测试方法End*******************************//  
      
}  
//*******************************测试方法Start*******************************//  
bool AStar::isPath(list<point> *path, int i, int j)  
{  
    for(list<point>::iterator openIterator = path->begin(); openIterator != path->end(); ++openIterator)  
    {  
        if((*openIterator).x == i && (*openIterator).y == j){return true;}  
    }  
    return false;  
}  
//*******************************测试方法End*******************************//  
//添加到开放列表  
void AStar::AddToOpenList(Node *src){  
    //添加当前坐标的周围坐标(一共8个方向)  
    GetNodeByPoint(src, src->pt.x, src->pt.y + 1);//上  
    GetNodeByPoint(src, src->pt.x, src->pt.y - 1);//下  
    GetNodeByPoint(src, src->pt.x - 1, src->pt.y);//左  
    GetNodeByPoint(src, src->pt.x + 1, src->pt.y);//右  
    GetNodeByPoint(src, src->pt.x - 1, src->pt.y + 1);//左上  
    GetNodeByPoint(src, src->pt.x - 1, src->pt.y - 1);//左下  
    GetNodeByPoint(src, src->pt.x + 1, src->pt.y + 1);//右上  
    GetNodeByPoint(src, src->pt.x + 1, src->pt.y - 1);//右下  
}  
void AStar::GetNodeByPoint(Node *pareNode, int _x, int _y)  
{  
    //如果坐标已经越界则不添加---具体参数读配置文件  
    if(_x < 0 || _y< 0 || _x >10 || _y>10)  
        return ;  
    Node *sNode = new Node();  
    //设定坐标值  
    sNode->pt.x = _x;  
    sNode->pt.y = _y;  
    //如果此坐标存在于封闭列表或是阻挡物的话,不进行添加  
    if(IsClosed(sNode) || IsBlock(sNode))  
    {  
        return;  
    }  
    else  
    {  
        //设定父结点  
        sNode->preNode = pareNode;  
        SetG(sNode);SetH(sNode);SetF(sNode);//设定各种耗费  
        psOpenList.push_front(sNode);//添加到开放列表  
        if(sNode->pt.x == sEndNode->pt.x && sNode->pt.y == sEndNode->pt.y)  
            pcurrNode = sNode;//终点坐标已经存在于开放列表  
    }  
}  
//是否存在于封闭列表  
bool AStar::IsClosed(Node *src)  
{  
    for(list<Node*>::iterator openIterator = psCloseList.begin(); openIterator != psCloseList.end(); ++openIterator)  
    {  
        if((*openIterator)->pt.x == src->pt.x && (*openIterator)->pt.y == src->pt.y){return true;}  
    }  
    return false;  
}  
//是否存在于开放列表  
bool AStar::IsInOpenList(Node *src)  
{  
    for(list<Node*>::iterator openIterator = psOpenList.begin(); openIterator != psOpenList.end(); ++openIterator)  
    {  
        if((*openIterator)->pt.x == src->pt.x && (*openIterator)->pt.y == src->pt.y){return true;}  
    }  
    return false;  
}  
//是否是阻挡坐标  
bool AStar::IsBlock(Node *src)  
{  
    //下面是测试数据,真实的数据去读配置文件  
    if(src->pt.x == 0 && src->pt.y == 4)return true;  
    if(src->pt.x == 1 && src->pt.y == 4)return true;  
    if(src->pt.x == 2 && src->pt.y == 4)return true;  
    if(src->pt.x == 3 && src->pt.y == 4)return true;  
    if(src->pt.x == 4 && src->pt.y == 4)return true;  
    //if(src->pt.x == 5 && src->pt.y == 4)return true;  
    if(src->pt.x == 6 && src->pt.y == 4)return true;  
    if(src->pt.x == 7 && src->pt.y == 4)return true;  
    if(src->pt.x == 8 && src->pt.y == 4)return true;  
    if(src->pt.x == 9 && src->pt.y == 4)return true;  
    if(src->pt.x == 10 && src->pt.y == 4)return true;  
    return false;  
}  
//计算从B点(某点)到终点的耗费  
void AStar::SetH(Node *node)  
{  
    node->h = (abs(node->pt.x - sEndNode->pt.x) + abs(node->pt.y - sEndNode->pt.y)) * SPEND_H;  
}  
//计算从起点到B点(某点)的耗费  
void AStar::SetG(Node *node)  
{  
    if(node->pt.x != node->preNode->pt.x && node->pt.y != node->preNode->pt.y)  
    node->g = node->preNode->g + SPEND_G_FAR;  
    else  
    node->g = node->preNode->g + SPEND_G_NEAR;  
}  
//计算总体耗费 F = G + H  
void AStar::SetF(Node *node)  
{  
    node->f = node->g + node->h;  
}  
//从开放列表中得到最优值(F值最低)  
Node* AStar::GetBestNode()  
{  
    Node* bNode;  
    int bestF = 9999999;  
    list<Node*>::iterator iterT;//记录最优值的位置方便删除  
    for(list<Node*>::iterator openIterator = psOpenList.begin(); openIterator != psOpenList.end(); ++openIterator)  
    {  
        if(bestF > (*openIterator)->f){  
            bestF = (*openIterator)->f; bNode = *openIterator;  
            iterT = openIterator;  
        }  
    }  
    if(bNode)  
        psOpenList.erase(iterT);//找到最优值后从开放列表中删除  
    return bNode;  
}  
 


测试文件


 


[cpp] view plaincopy
/************************************************************************/  
/* author: 小鲁 
/* version: 1.0 
/* update: 06/26/10 
/************************************************************************/  
#include "stdafx.h"  
#include "iostream"  
#include <list>  
#include "AStar.h"  
using namespace std;  
/************************************************************************/  
/* A 星算法测试                                                                     */  
/************************************************************************/  
int _tmain(int argc, _TCHAR* argv[])  
{  
    point sp;  
    sp.x = 1;  
    sp.y = 3;  
    point ep;  
    ep.x = 10;  
    ep.y = 10;  
      
    AStar astar;  
    astar.SetStartNode(sp);  
    astar.SetEndNode(ep);  
    astar.Search();  
      
    getchar();  
    return 0;  
}  
0 0