A*寻路算法c++

本文介绍了一种基于A*算法的路径寻找实现方法。该方法通过定义特定的数据结构和逻辑流程,在给定的二维网格环境中寻找从起点到终点的有效路径。文章详细描述了初始化路径、更新数据、排序节点等关键步骤,并展示了如何使用C++实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  Algorithm_A_Self.cpp

//  StudyTest

//

//  Created by metrics001 on 16/11/9.

//

//


#include "Algorithm_A_Self.hpp"

#define DIS  50

#define boardDIS  140

#define boardDIS2  260


static POSTION allPOSTION[25];

static const POINT* point_head;


static vector*> pointList;


void Algorithm_A_Self::init_path(Layer* layer)

{

    

    int data[5][5] = {

        {1,1,1,1,1},

        {1,1,1,0,1},

        {1,1,1,0,1},

        {1,0,0,1,1},

        {1,1,1,1,1}

    };

    int index = 0;

    int n =5;int m =5;

    for (int i =0;i

        for (int j =0;j

            allPOSTION[index].x = j;

            allPOSTION[index].y = i;

            allPOSTION[index].value = data[i][j];

            index++;

        }

    }

    for (int i = 0; i<</span>25; i++) {

        Sprite* spr = Sprite::create("CloseNormal.png");

        layer->addChild(spr);

        spr->setPosition(Vec2(boardDIS+allPOSTION[i].x*DIS,boardDIS2-allPOSTION[i].y*DIS));

        if (allPOSTION[i].value==0) {

            spr->setColor(Color3B(255,0,0));

        }

    }

    

}


int Algorithm_A_Self::get_length(POINT* cur)

{

    int length = 1;

    POINT* point = cur;

    while (point->m_last!=nullptr) {

        point = point->m_last;

        length++;

    }

    return length;

}



bool Algorithm_A_Self::check_pos_valid(POSTION pos)

{

    for (int i = 0; i<</span>25; i++) {

        if (pos.x == allPOSTION[i].x&&pos.y == allPOSTION[i].y) {

            if (allPOSTION[i].value==0) {

                return  false;

            }

            return true;

        }

    }

    return false;

}



void Algorithm_A_Self::handle_point(POINT* cur,POSTION pos,vector* nextList)

{

    

    POINT* point = check_bool_at_path(pos,point_head);

    if (point==nullptr) {

        POINT* pData = new POINT();

        pData->m_pos = pos;

        cur->m_next->push_back(pData);

        pData->m_last = cur;

        pData->m_length = get_length(pData);

        //新加的放入下次搜索列表中

        nextList->push_back(pData);

    }else

    {

        

        point->m_length = get_length(point);

        cur->m_length = get_length(cur);

        if (point->m_length >cur->m_length+1) {

            

            POINT* last = point->m_last;

            point->m_last = cur;

            cur->m_next->push_back(point);

            if (last==nullptr) {

                return;

            }

            

            vector<_POINT*>* next = last->m_next;

            int index = 0;

            for (auto iter = next->begin(); iter != next->end(); iter++) {

                

                if (*iter==point) {

                    next->erase(next->begin()+index);

                    break;

                }

                index++;

            }

        }

        

    }

    

}


vector* Algorithm_A_Self::updata_data_for_queue(vector* points)

{

    if(NULL == points )

        return nullptr;

    vector* nextList = new vector();

    

    for(int index = 0; index < points->size(); index ++){

        

        POINT* data = points->at(index);

        if ( data->m_open==false) {  //检测过了,有点远

            continue;

        }

        

        vector* pointList = new vector();

        data->m_next = pointList;

        if(check_pos_valid(POSTION(data->m_pos.x, data->m_pos.y - 1))){

            handle_point(data,POSTION(data->m_pos.x, data->m_pos.y - 1),nextList);

        }

        

        if(check_pos_valid(POSTION(data->m_pos.x -1, data->m_pos.y))){

            handle_point(data,POSTION(data->m_pos.x -1, data->m_pos.y),nextList);

        }

        

        if(check_pos_valid(POSTION(data->m_pos.x, data->m_pos.y + 1))){

            handle_point(data,POSTION(data->m_pos.x, data->m_pos.y + 1),nextList);

        }

        

        if(check_pos_valid(POSTION(data->m_pos.x + 1, data->m_pos.y))){

            handle_point(data,POSTION(data->m_pos.x + 1, data->m_pos.y),nextList);

        }

    }

//    points->clear();

//    delete points;

    return nextList;

}


POINT* Algorithm_A_Self::check_bool_at_path(POSTION pos,const POINT* head)

{

    const POINT* cur = head;

    if (cur->m_pos.x==pos.x&&cur->m_pos.y==pos.y) {

//        printf("已经存在了\n");

        return (POINT*)cur;

    }

    if (cur->m_next == nullptr || cur->m_next->size()==0) {

        return nullptr;

    }

    for (auto iter = cur->m_next->begin(); iter != cur->m_next->end(); iter++) {

        POINT* point =  check_bool_at_path(pos,*iter);

        if (point!=nullptr) {

             return point;

        }

    }

    return nullptr;

}


//    得到估算距离最近

POINT* Algorithm_A_Self::sort_most_nearest_neigh(vector* pointList,POSTION end)

{

    //排序优先查找最小的

    sort(pointList->begin(),pointList->end(),[=](POINT* p1,POINT* p2){

                int dis1 = abs(p1->m_pos.x - end.x)+ abs(p1->m_pos.y - end.y) + get_length(p1);

                int dis2 = abs(p2->m_pos.x - end.x)+ abs(p2->m_pos.y - end.y)+ get_length(p2);;

        

//        point->m_length = get_length(point);

//        cur->m_length = get_length(cur);

        

//        float dis1 = (float) sqrt((p1->m_pos.x - end.x) * (p1->m_pos.x - end.x)+ (p1->m_pos.y - end.y) *  (p1->m_pos.y - end.y));

//        float dis2 = (float) sqrt((p2->m_pos.x - end.x) * (p2->m_pos.x - end.x)+ (p2->m_pos.y - end.y) *  (p2->m_pos.y - end.y));

        if (dis1

            return true;

        }

        return false;

    });

    return pointList->at(0);

}

POINT* Algorithm_A_Self::check_path(POINT* point,POSTION end)

{

    

    vector* points = new vector();

    points->push_back(point);

    

    while (true) {

        if (points->size()==0) {

            printf("此路不通\n");

            return nullptr;

        }

        pointList.push_back(points);

        //    //排序下一级的点

        POINT* min = sort_most_nearest_neigh(points,end);

         //    //关闭远的点

        int dis1 = abs(min->m_pos.x - end.x)+ abs(min->m_pos.y - end.y) + get_length(min);

        for (int i = 0;isize(); i++) {

            POINT *point = points->at(i);

            int dis = abs(point->m_pos.x - end.x)+ abs(point->m_pos.y - end.y) + get_length(point);

            if (dis>dis1) {

                point->m_open=false;

                break;

            }

        }

        

        for (auto iter = points->begin(); iter != points->end(); iter++) {

            POINT* cur = *iter;

            if (cur->m_pos.x==end.x&&cur->m_pos.y==end.y) {

                printf("找到目标了\n");

                return cur;

            }

        }

    

        points = updata_data_for_queue(points);

        

        printf("count:%d\n",(int)points->size());

        

    }

    return nullptr;

    

    

//    POINT* cur = point;

//    if (cur->m_pos.x==end.x&&cur->m_pos.y==end.y) {

//        printf("找到目标了\n");

//        return cur;

//    }

//    vector* points = updata_data_for_queue(cur);

//    if (points->size()==0) {

//        

//        printf("此路不通\n");

//         printf("%d,%d,\n",cur->m_pos.x,cur->m_pos.y);

//        return nullptr;

//    }

//    

//    

//    //排序下一级的点

//   POINT* min = sort_most_nearest_neigh(points,end);

//    

//    for (auto iter = points->begin(); iter != points->end(); iter++) {

//        POINT* cur = *iter;

//        POINT* point =check_path(cur, end);

//        if (point!=nullptr) {

//            printf("找到目标了\n");

//            return point;

//        }

//    }

//    

////   return check_path(min,end);

//    

//    POINT* reult = nullptr;

//    for (auto iter = cur->m_next->begin(); iter != cur->m_next->end(); iter++) {

//        POINT* _reult = check_path(*iter,end);

//        if (_reult!=nullptr) {

//             return _reult;

////            reult =  _reult;

//        }

//    }

//    return reult;

}

void Algorithm_A_Self::find_path(POSTION begin,POSTION end,Layer* layer)

{


    POINT*head = new POINT();

    point_head = head;

    POINT*cur = head;

    cur->m_pos = begin;

    POINT* end_pint = check_path(cur,end);

    if (end_pint == nullptr) {

        printf("没有找到可行路径\n");

        return ;

    }

//    routePOSTION 即为路径

    POINT* point = end_pint;

    vector routePOSTION;

    routePOSTION.push_back(point->m_pos);

    while (point->m_last!=nullptr) {

        point = point->m_last;

        routePOSTION.push_back(point->m_pos);

    }

    

    int count = pointList.size();

    for (int i = 0; i

        vector* points = pointList.at(i);

        for (int j = 0; jsize();j++) {

            POINT* point = points->at(j);

            

            Sprite* spr = Sprite::create("ui_b_next.png");

            layer->addChild(spr,2);

            spr->setPosition(Vec2(boardDIS+point->m_pos.x*DIS,boardDIS2-point->m_pos.y*DIS));

            spr->setVisible(false);

            spr->runAction(Sequence::create(DelayTime::create(i*1),CallFunc::create([=](){spr->setVisible(true);}),nullptr));

            spr->setColor(Color3B(60,60,60));

            

            Size size = spr->getContentSize();

            auto label = Label::createWithTTF(__String::createWithFormat("%d",i)->getCString(),"fonts/Marker Felt.ttf", 12);

            label->setPosition(Vec2( size.width/2, size.height/2));

            spr->addChild(label);

            

        }

        

    }

    

//    int count = (int)routePOSTION.size();

//    for (int i = 0; i

//        Sprite* spr = Sprite::create("ui_b_next.png");

//        layer->addChild(spr,2);

//        spr->setPosition(Vec2(boardDIS+routePOSTION.at(count-1-i).x*DIS,boardDIS2-routePOSTION.at(count-1-i).y*DIS));

//        spr->setVisible(false);

//        spr->runAction(Sequence::create(DelayTime::create(i*1),CallFunc::create([=](){spr->setVisible(true);}),nullptr));

//    }

//    

    Sprite* spr = Sprite::create("ui_record.png");

    layer->addChild(spr,1);

    spr->setPosition(Vec2(boardDIS+end.x*DIS,boardDIS2-end.y*DIS));



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值