也许我误解了目标,但这似乎很简单,不需要专门的库(除此之外首先要创建网格)。这是我使用 boost 图形库制作的递归版本,它与网格的结构无关。
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
int, boost::no_property, boost::no_property, boost::vecS> myGraph;
typedef boost::graph_traits<myGraph>::adjacency_iterator adj_iter;
void GetPaths(const myGraph& g, int max_size, const std::vector<int>& path, std::vector<std::vector<int>>& paths)
{
//for each adjacent vertex to the last vertex in 'path'
for (std::pair<adj_iter, adj_iter> ap = boost::adjacent_vertices(path.back(), g); ap.first != ap.second; ++ap.first)
{
//if the path contains the vertex, continue to the next adjacent vertex
if (std::find(path.begin(), path.end(), g[*ap.first]) != path.end()) { continue; }
//otherwise, make a copy of the path with space for an extra vertex (you may need 'path' for the other adjacent vertices
std::vector<int> new_path(path.size() + 1);
std::copy(path.begin(), path.end(), new_path.begin());
new_path.back() = g[*ap.first];
//whatever your termination conditions are
if (new_path.size() == max_size)
{
paths.push_back(new_path);
}
//otherwise, go deeper
else
{
GetPaths(g, max_size, new_path, paths);
}
}
}
为了适应您的问题,我建议将std::pair<std::vector<int>,double>其用作路径/长度对象。对于图形的边缘属性,您将有一个双重表示距离。终止条件将是最大步数或路径的累积长度。
您可以通过为不完整路径创建一个容器,甚至可能是一个 tbb::concurrent_vector 来使其成为非递归的,这样您就可以对其进行并行化。