【问题标题】:How can i find the shortest path form a[x][y] to any a[v][w]?如何找到从 a[x][y] 到任何 a[v][w] 的最短路径?
【发布时间】:2014-03-19 05:42:36
【问题描述】:

我有这样的东西。

---a-

aaa--

一个----

---a-

b--a-

如何以最少的步骤从 b 到达所有 a? 我该如何用图表来解决这个问题?

【问题讨论】:

  • ^ 这很昂贵。这可以使用简单的 BFS(级别顺序遍历)来解决
  • @user2849394:到目前为止你尝试了什么?
  • 我不知道如何将此矩阵表示为图形..这就是问题..

标签: c++ c graph multidimensional-array shortest-path


【解决方案1】:

a* algorithm 可能会为您服务。它的变化是在游戏开发中寻找最短(成本最低的路径)的常见解决方案。至于图形表示,定义一个称为节点和连接的结构。您可以使用 std::vector 存储顶点和 std::vector 连接(边)。

typedef struct 
{
  std::string name;
  //Other data that You need.
}Node;

typedef struct
{
  Node* nodeA;
  Node* nodeB;
  int travelCost;
  //other data that may come in handy
}Connection;

std::vector<Node> nodes;
std::vector<Connection> nodeConnections;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多