#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <deque>
#include <boost/graph/adjacency_list.hpp>
//A*寻路算法
#include <boost\graph\astar_search.hpp>
using namespace std;
using namespace boost; enum { A, B, C, D, E, N };
string Names = "ABCDE";
//定义别名,两个顶点直接连接的边
using Edge = pair<int, int>;
//创建一个图 边 顶点 有方向 无特性 边的权重是int
using Graph = adjacency_list<listS, vecS, directedS, no_property, property<edge_weight_t, int>>; //创建一个图
Graph make_graph()
{
//连接的边
vector<Edge> edges = { {A,B},
{A,C},
{A,D},
{B,E},{C,E},{D,E} };
//边对应的权重
vector<int> weight = { ,,,,, }; //创建一个图对象
return Graph(edges.begin(), edges.end(), weight.begin(), N);
} //创建一个结构体,用于抛出找到信息
struct found_goal
{ }; //A*要到达的目标顶点
template<class vertex>
class astar_my_visitor :public boost::default_astar_visitor
{
public:
//初始化内置地图
astar_my_visitor(vertex goal) :m_goal(goal)
{ }
//重载examine_vertex方法
template<class Graph>
void examine_vertex(vertex v, Graph &g)
{
//如果与目标顶点一样,则说明找到
if (v == m_goal)
{
//抛出抛出找到信息
throw found_goal();
}
}
private:
//目标顶点
vertex m_goal;
}; //计算权重寻找最短路径
template<class Graph,class costtype>
class distance_heuristic :public boost::astar_heuristic<Graph, costtype>
{
public:
//类型替换
using Vertex = typename boost::graph_traits<Graph>::vertex_descriptor; //初始化
distance_heuristic(Vertex Goal, Graph &graph):Goal_(Goal),graph_(graph)
{ } //重载()运算符 获得目标点到指定点的距离
costtype operator()(Vertex v)
{
return get(vertex_index, graph_, Goal_) - get(vertex_index, graph_, v);
} private:
Vertex Goal_;
Graph &graph_;
}; void main()
{
//创建图
Graph myg = make_graph(); //创建简写
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using Cost = int; Vertex start = vertex(A, myg);//开始位置
Vertex goal = vertex(E, myg);//结束位置 //保存走过路径(由后向前)
vector<Vertex>parents(boost::num_vertices(myg));
//保存长度
vector<Cost>distance(boost::num_vertices(myg)); try
{
//求从指定点到终点的路线 boost::astar_search_tree(myg,
start,
distance_heuristic<Graph, Cost>(goal, myg),//传递距离 //求出路径,以及路径对应的权重,访问器访问 因为重载了()运算符
boost::predecessor_map(&parents[]).distance_map(&distance[]).visitor(astar_my_visitor<Vertex>(goal))
);
}
//catch信息
catch (found_goal fg)
{
//要到的位置的前一个到达的位置如果是goal(下标是当前点,值是到这个点之前的点)
if (parents[goal] == goal)
{
cout << "无路可走" << endl;
}
deque<Vertex> route; //顺藤摸瓜
for (Vertex v = goal; v != start; v = parents[v])
{
route.push_front(v);
}
for (auto i : route)
{
cout << Names[i] << endl;
}
} system("pause");
}

19.boost A*算法的更多相关文章

  1. boost字符串算法

    boost::algorithm简介 2007-12-08 16:59 boost::algorithm提供了很多字符串算法,包括: 大小写转换: 去除无效字符: 谓词: 查找: 删除/替换: 切割: ...

  2. boost之算法

    STL里的算法已经很好了,在boost里有几个小的算法 1.BOOST_FOREACH使用方法,定义一个容器里内部类型数据,容器作为参数传递. #include <iostream> #i ...

  3. paper 19 :机器学习算法(简介)

    本来看了一天的分类器方面的代码,乱乱的,索性再把最基础的概念拿过来,现总结一下机器学习的算法吧! 1.机器学习算法简述 按照不同的分类标准,可以把机器学习的算法做不同的分类. 1.1 从机器学习问题角 ...

  4. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  5. 19.-哈希算法&注册登录

    一.哈希算法 哈希: 给定明文-计算出一段定长的-不可逆的值 定长输出:不管明文输入多少,哈希都是定长的 不可逆:无法反向计算出对应的明文 雪崩效应:输入改变,输出必然变 md5:32位16进制   ...

  6. Dlib 19.4(算法,压缩,图像处理,机器学习,Meta编程,网络,HTTP服务器)

    Algorithms API Wrappers Bayesian Nets Compression Containers Graph Tools Image Processing Linear Alg ...

  7. 19道常见的JS面试算法题

    最近秋招也做了多多少少的面试题,发现除了基础知识外,算法还是挺重要的.特意整理了一些常见的算法题,添加了自己的理解并实现. 除此之外,建议大家还可以刷刷<剑指offer>.此外,左神在牛客 ...

  8. Adaboost 算法

    一 Boosting 算法的起源 boost 算法系列的起源来自于PAC Learnability(PAC 可学习性).这套理论主要研究的是什么时候一个问题是可被学习的,当然也会探讨针对可学习的问题的 ...

  9. 浅谈 Adaboost 算法

    http://blog.csdn.net/haidao2009/article/details/7514787 菜鸟最近开始学习machine learning.发现adaboost 挺有趣,就把自己 ...

随机推荐

  1. iOS~判断应用是否有定位权限

    在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...

  2. php静态函数的使用场景

    php静态函数的使用场景 场景 代码 <?php class Conductor{ public static $i = 100; public function sold(){ $a = se ...

  3. 对于NAS,IP SAN以及iSCSCI SAN存储的一些认识和理解

    一直以来用户对于在选购存储产品上有许多不清楚,市场上有NAS, FC SAN,IP SAN和iSCSCI SAN产品,到底哪种类型的产品更适合支撑企业的应用系统呢? 我们经常可以听到用户讲: “NAS ...

  4. USACO 1.4 Mother's Milk

    Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numb ...

  5. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd

  6. caffe下python环境的编译

    安装python所需的依赖包 (1)sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-n ...

  7. CentOS 安装 PHP 扩展

    下载地址:https://pecl.php.net/package/redis 上传目录:/usr/local/src //安装依赖 yum install php-devel -y //进入安装包目 ...

  8. GreenDao 3.X之基本使用

    在GreenDao 3.X之注解已经了解到GreenDao 3.0的改动及注解.对于数据库的操作,无异于增删改查等四个操作.下面我们将了解GreenDao 3.X如何使用? AbstractDao 所 ...

  9. 对ListView的Item子控件监听并跳转页面

    public class MyAdapteforOwner extends BaseAdapter{ List<OwnerDevice>datas; private Context con ...

  10. 页面加载通过javascript来修改控件属性

    function changeFormElementStatus(tagNames) {            var tagNameArr = tagNames.split("," ...