0

我正在为游戏制作一个 GPS 系统,它可以让你选择道路上两点之间的最短路径。

至于现在,我做了一个如下所示的课程:

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>

using namespace boost;
using namespace std;

class GPS
{
public:
    typedef         boost::property<boost::edge_weight_t, float>                        Distance;
    typedef         adjacency_list<vecS, vecS, directedS, boost::no_property, Distance> Graph;
    typedef         int                                                                 Node;
    typedef         std::pair<int, int>                                                 Edge;
    typedef         property_map<Graph, edge_weight_t>::type                            weightmap_t;                
    typedef         graph_traits < Graph >::vertex_descriptor                           vertex_descriptor;
    typedef         graph_traits < Graph >::edge_descriptor                             edge_descriptor;
private:
    vector<Edge>                            Edges;
    Graph                                   Nodes;
public:
    GPS() 
    {

    }
    ~GPS()
    {

    }
    //returns amount of edges added: 0, 1 or 2
    char AddEdge(Node from, Node to, Distance weight = 0.0f, bool BothDirections = false)
    {
        char added = 0;
        if(add_edge(from,to,weight,Nodes).second)
            ++added;
        if(BothDirections)
        {
            if(add_edge(to,from,weight,Nodes).second)
                ++added;
        }
        return added;
    }
    //returns the added node, 
    //specify your own vertex identificator if wanted 
    //(for maintaining backwards compatibility with old graphs saved in gps.dat files)
    Node AddNode(int id = -1)
    {
        if(id == -1)
            return add_vertex(Nodes);
        else
            return vertex(id,Nodes);
    }
    //get the shortest path between 'from' and 'to' by adding all nodes which are traversed into &path
    void Path(Node from, Node to, vector<Node> &path)
    {
        std::vector<vertex_descriptor> p(num_vertices(Nodes));
        std::vector<int> d(num_vertices(Nodes));
        weightmap_t weightmap = get(edge_weight, Nodes);
        vertex_descriptor s = vertex(from, Nodes);
        dijkstra_shortest_paths(Nodes, s, predecessor_map(&p[0]).distance_map(&d[0]));

        //what here? and are there faster algorithms in boost graph than dijkstra (except A*)?
    }
};

现在,当要找到两个顶点之间的路径时,我真的陷入了困境。

我查找了 dijkstra 的文档示例,但我就是不明白..

任何其他算法似乎都更难设置。

如何找到最短路径?所有的参数和功能和东西都非常混乱..我想切换到boost,远离“我自己的家常菜和慢”库..

4

1 回答 1

0

这段代码将为您提供对于每个节点,您必须遵循哪个节点才能到达源,遵循最短路径:(取自 Boost 中的示例代码)

  std::cout << "distances and parents:" << std::endl;
    graph_traits < graph_t >::vertex_iterator vi, vend;
    for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
        std::cout << "distance(" << *vi << ") = " << d[*vi] << ", ";
        std::cout << "parent(" << *vi << ") = " << p[*vi] << std::
        endl;
    }

所以你所要做的就是去做

 n= dest;
 while (n!=src) {
 path.push_back(n);
 n = p[n]; // you're one step closer to the source.. 
}
于 2013-06-26T20:11:07.443 回答