19.boost A*算法
#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*算法的更多相关文章
- boost字符串算法
boost::algorithm简介 2007-12-08 16:59 boost::algorithm提供了很多字符串算法,包括: 大小写转换: 去除无效字符: 谓词: 查找: 删除/替换: 切割: ...
- boost之算法
STL里的算法已经很好了,在boost里有几个小的算法 1.BOOST_FOREACH使用方法,定义一个容器里内部类型数据,容器作为参数传递. #include <iostream> #i ...
- paper 19 :机器学习算法(简介)
本来看了一天的分类器方面的代码,乱乱的,索性再把最基础的概念拿过来,现总结一下机器学习的算法吧! 1.机器学习算法简述 按照不同的分类标准,可以把机器学习的算法做不同的分类. 1.1 从机器学习问题角 ...
- C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构
STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...
- 19.-哈希算法&注册登录
一.哈希算法 哈希: 给定明文-计算出一段定长的-不可逆的值 定长输出:不管明文输入多少,哈希都是定长的 不可逆:无法反向计算出对应的明文 雪崩效应:输入改变,输出必然变 md5:32位16进制 ...
- Dlib 19.4(算法,压缩,图像处理,机器学习,Meta编程,网络,HTTP服务器)
Algorithms API Wrappers Bayesian Nets Compression Containers Graph Tools Image Processing Linear Alg ...
- 19道常见的JS面试算法题
最近秋招也做了多多少少的面试题,发现除了基础知识外,算法还是挺重要的.特意整理了一些常见的算法题,添加了自己的理解并实现. 除此之外,建议大家还可以刷刷<剑指offer>.此外,左神在牛客 ...
- Adaboost 算法
一 Boosting 算法的起源 boost 算法系列的起源来自于PAC Learnability(PAC 可学习性).这套理论主要研究的是什么时候一个问题是可被学习的,当然也会探讨针对可学习的问题的 ...
- 浅谈 Adaboost 算法
http://blog.csdn.net/haidao2009/article/details/7514787 菜鸟最近开始学习machine learning.发现adaboost 挺有趣,就把自己 ...
随机推荐
- iodine免费上网——本质就是利用dns tunnel建立tcp,然后tcp proxy来实现通过访问虚拟dns0网卡来访问你的dns 授权server
我的命令: server端: sudo iodined -P passwd -f -DD 10.0.0.100 abc.com client端(直连模式,-r表示使用xxx.abc.com的xxx来转 ...
- 【转】用CocoaPods做iOS程序的依赖管理 -- 不错
原文网址:http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/ 文档更新说明 2012-12-02 ...
- 限制textfield的文字长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementSt ...
- Vue模拟酷狗APP问题总结
一.NewSongs.vue中的38行 this.$http.get('/proxy/?json=true') 里面这个路径的获取 二.router文件夹中的index.js 中的 comp ...
- 安卓input框获取焦点时,底部按钮会顶上去的解决方法
var h = document.body.scrollHeight;window.onresize = function(){ if (document.body.scrollHeight < ...
- Linux中设置vim自动在运算符号两边加上空格
vim中设置自动在=+-之类的运算符号左右两边加上空格.原版的vim不带这个功能,写出的代码例如z=x+y,不美观,很多编译器(如VS)能够自动在符号两边加上空格,如z = x + y,看起来比较美观 ...
- angular自定义指令-directive
Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...
- RocketMQ学习笔记(11)----RocketMQ的PushConsumer和PullConsumer
1. PushConsumer 推,Broker主动向Consumer推消息,它Consumer的一种,应用通常向对象注册一个Listener接口,一旦接收到消息,Consumer对象立刻回调Lins ...
- jsp+jdbc实现用户登录
1.1 创建数据库表 表名:user 字段: userid 保存用户的登录id name 用户名 password 密码 1.2 实现思路 a. 用户登录,则需要有个一个表单页,此页面可输 ...
- fullcalendar日历插件
https://www.helloweba.net/javascript/231.html