A*寻路算法c++

//

//  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));



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C语言实现的A*寻路算法: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ROW 5 #define COL 5 typedef struct { int x, y; } Point; typedef struct { Point parent; int f, g, h; } Node; Node map[ROW][COL]; Point start, end; bool is_valid(int x, int y) { return x >= 0 && x < ROW && y >= 0 && y < COL; } bool is_obstacle(int x, int y) { return map[x][y].f == -1; } int heuristic(int x, int y) { return abs(x - end.x) + abs(y - end.y); } void print_path() { Point path[ROW * COL]; int count = 0; Point current = end; while (current.x != start.x || current.y != start.y) { path[count++] = current; current = map[current.x][current.y].parent; } path[count++] = start; printf("Path: "); for (int i = count - 1; i >= 0; i--) { printf("(%d, %d) ", path[i].x, path[i].y); } printf("\n"); } void a_star() { int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; map[start.x][start.y].f = 0; map[start.x][start.y].g = 0; map[start.x][start.y].h = heuristic(start.x, start.y); Node open_set[ROW * COL]; int open_set_size = 1; open_set[0] = map[start.x][start.y]; while (open_set_size > 0) { int current_index = 0; for (int i = 1; i < open_set_size; i++) { if (open_set[i].f < open_set[current_index].f) { current_index = i; } } Node current = open_set[current_index]; if (current.parent.x == end.x && current.parent.y == end.y) { print_path(); return; } open_set[current_index] = open_set[open_set_size - 1]; open_set_size--; for (int i = 0; i < 8; i++) { int new_x = current.parent.x + dx[i]; int new_y = current.parent.y + dy[i]; if (!is_valid(new_x, new_y) || is_obstacle(new_x, new_y)) { continue; } int new_g = current.g + 1; int new_h = heuristic(new_x, new_y); int new_f = new_g + new_h; if (map[new_x][new_y].f == -2 || new_f < map[new_x][new_y].f) { map[new_x][new_y].parent = current.parent; map[new_x][new_y].f = new_f; map[new_x][new_y].g = new_g; map[new_x][new_y].h = new_h; if (map[new_x][new_y].f == -2) { open_set[open_set_size++] = map[new_x][new_y]; } } } } printf("No path found!\n"); } int main() { // initialize map for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { map[i][j].parent.x = i; map[i][j].parent.y = j; map[i][j].f = -2; map[i][j].g = -2; map[i][j].h = -2; } } // set obstacles map[1][1].f = -1; map[1][2].f = -1; map[1][3].f = -1; map[2][3].f = -1; map[3][3].f = -1; map[3][2].f = -1; // set start and end points start.x = 0; start.y = 0; end.x = 4; end.y = 4; // run A* algorithm a_star(); return 0; } ``` 这个程序实现了一个简单的A*寻路算法,可以在一个5x5的地图上找到从起点到终点的最短路径。其中,-1表示障碍物,-2表示未访问过的节点,其它数字表示已访问过的节点的f值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值