美文网首页
算法 & 数据结构——A*寻路

算法 & 数据结构——A*寻路

作者: 落单的毛毛虫 | 来源:发表于2017-05-22 00:33 被阅读0次

A*是一种很常见的游戏寻路算法。

长久以来,我一直觉得A*是一种高大上的算法,因为听很多人说起此算法,然而,江湖一张纸,戳穿不值一毛钱,思路其实很简单通过估值函数决定下一步走向

估值函数

  • 决定整个算法的效率,以及准确率。
  • 可以根据实际需求,使用不同的估值函数。

实现

#pragma once

#include "base.h"
#include "math.h"
#include "priority_queue.h"

class AStar {
public:
    struct Box {
        MATH Vec2 parent;
        MATH Vec2 self;
        int G, H;
        int F() const
        {
            return G + H;
        }

        Box()
        { }

        explicit Box(const MATH Vec2 & pt1, const MATH Vec2 & pt2)
            : parent(pt1)
            , self(pt2)
        { }

        explicit Box(const MATH Vec2 & pt1, const MATH Vec2 & pt2, int g, int h)
            : parent(pt1)
            , self(pt2)
            , G(g)
            , H(h)
        { }

        bool operator==(const MATH Vec2 & point) const
        {
            return MATH equal(self, point);
        }

        bool operator<(const Box & other) const
        {
            return F() > other.F();
        }
    };

    using NextFunc = STD function<void(const MATH Vec2 &)>;
    using TestFunc = STD function<bool(const MATH Vec2 &)>;
public:
    AStar()
    { }

    ~AStar()
    { }

    bool IsFinish()
    {
        return _openList.empty() || MATH equal(_end, _openList.top().self);
    }

    const MATH Vec2 & GetQueryBeg() const
    {
        return _beg;
    }

    const MATH Vec2 & GetQueryEnd() const
    {
        return _end;
    }

    void SetQueryPoint(const MATH Vec2 & beg, const MATH Vec2 & end)
    {
        _beg = beg;
        _end = end;
        _closeList.clear();
        _openList = priority_queue<Box>();
        _openList.emplace(beg, beg, 0, 0);
    }

    bool GetQueryPath(STD vector<MATH Vec2> & path)
    {
        if (!MATH equal(_end, _closeList.back().self))
        {
            return false;
        }
        auto parent = _closeList.back().self;
        for (; !_closeList.empty(); _closeList.pop_back())
        {
            if (MATH equal(parent, _closeList.back().self))
            {
                path.push_back(_closeList.back().self);
                parent = _closeList.back().parent;
            }
        }
        return true;
    }

    void SetTestFunc(const TestFunc & func)
    {
        _testFunc = func;
    }

    void SetNextFunc(const NextFunc & func)
    {
        _nextFunc = func;
    }

    bool QueryNext()
    {
        static const MATH Vec2 s_offset[] = {
            { 0, -1 },{ 1, 0 },{ 0, 1 },{ -1, 0 },          //  上,右,下,左。
            //{ 1, -1 },{ 1, 1 },{ -1, 1 },{ -1,-1 },           //  右上,右下,左下,左上。
        };
        
        if (!MATH equal(_end, _openList.top().self))
        {
            auto top = _openList.top();
            _closeList.push_back(top);
            _nextFunc(top.self);
            _openList.pop();
            for (auto &offset : s_offset)
            {
                CheckPoint(top, MATH add(top.self, offset));
            }
        }

        if (!_openList.empty() && MATH equal(_end, _openList.top().self))
        {
            _closeList.push_back(_openList.top());
            _nextFunc(_openList.top().self);
        }

        return IsFinish();
    }

private:
    void CheckPoint(const Box & from, const MATH Vec2 & to)
    {
        if (Test(to))
        {
            _openList.emplace(from.self, to, from.G + 1, Distance(to));
        }
    }

    int Distance(const MATH Vec2 & point)
    {
        return (int)(STD abs(point.x - _end.x) + STD abs(point.y - _end.y));
    }

    bool Test(const MATH Vec2 & point)
    {
        return _testFunc(point)
            && STD find(_openList.begin(), _openList.end(), point) == _openList.end()
            && STD find(_closeList.begin(), _closeList.end(), point) == _closeList.end();
    }

private:
    MATH Vec2 _beg;
    MATH Vec2 _end;
    NextFunc _nextFunc;
    TestFunc _testFunc;
    priority_queue<Box> _openList;
    STD vector<Box> _closeList;

private:
    AStar(const AStar &) = delete;
    AStar(AStar &&) = delete;
    AStar operator=(const AStar &) = delete;
    AStar operator=(AStar &&) = delete;

};

效果

A*寻路

DEMO下载

相关文章

  • 算法 & 数据结构——A*寻路

    A*是一种很常见的游戏寻路算法。 长久以来,我一直觉得A*是一种高大上的算法,因为听很多人说起此算法,然而,江湖一...

  • 百度无人驾驶apollo项目路径规划a*算法分析

    算法分析 车辆路径规划寻路算法有很多,apollo路径规划模块使用的是启发式搜索算法A*寻路算法。 a*算法是一种...

  • Unity学习笔记——A*寻路算法的应用

    初步了解了一些寻路算法后,本以为dijstra是比较合适的寻路算法,今天仔细看了关于A星寻路算法的教程和视频后,我...

  • 算法:A*寻路算法

    A*Search,是一种寻找有效路径的算法。 [OpenList][CloseList][F = G + H]Op...

  • JPS寻路算法

    JPS寻路算法是啥?JPS全称是:jump point search,这个算法实际上是对A* 寻路算法的一个改进,...

  • Hello,a~*寻路算法!

    寻路算法是游戏中经常用到的算法之一,而这其中A~* 算法大概是我们最耳熟的寻路算法了,下面我们会通过A~* 算法与...

  • A*寻路算法

    原文:http://www.cnblogs.com/wangnfhy/p/4956711.html 参考:http...

  • A*寻路算法

    代码实现

  • A* 寻路算法

    A 算法*是一种解决图遍历问题的计算机算法,在电子游戏中最主要的应用是寻找地图上两点间的最佳路线。 不能朝障碍物所...

  • cocos creator Astar寻路导航与地图编辑

    1、插件或者TileMap工具生成地图json文件 2、astar寻路算法 3、将json文件与寻路算法结合,获得...

网友评论

      本文标题:算法 & 数据结构——A*寻路

      本文链接:https://www.haomeiwen.com/subject/anzvxxtx.html