### C++中的路径规划算法实现
#### A* 算法概述
A* 是一种启发式的路径搜索算法,广泛应用于机器人导航、游戏开发等领域。它通过综合考虑实际代价和估计代价来寻找最优路径[^1]。
以下是基于 C++ 的 A* 算法实现:
```cpp
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
struct Node {
int x, y;
double g_cost, h_cost; // 实际成本和估算成本
pair<int, int> parent;
double f_cost() const { return g_cost + h_cost; }
};
// 计算曼哈顿距离作为 H 值
double heuristic(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
bool operator<(const Node& a, const Node& b) {
return a.f_cost() > b.f_cost();
}
vector<pair<int, int>> findPath(vector<vector<bool>>& grid, pair<int, int> start, pair<int, int> goal) {
priority_queue<Node> openSet;
vector<vector<bool>> closedSet(grid.size(), vector<bool>(grid[0].size(), false));
vector<vector<pair<int, int>>> parents(grid.size(), vector<pair<int, int>>(grid[0].size(), {-1, -1}));
openSet.push(Node{start.first, start.second, 0, heuristic(start.first, start.second, goal.first, goal.second), {-1, -1}});
while (!openSet.empty()) {
Node current = openSet.top(); openSet.pop();
if (current.x == goal.first && current.y == goal.second) {
vector<pair<int, int>> path;
while (!(current.parent.first == -1 && current.parent.second == -1)) {
path.emplace_back(current.x, current.y);
current.x = current.parent.first;
current.y = current.parent.second;
}
reverse(path.begin(), path.end());
return path;
}
closedSet[current.x][current.y] = true;
static const vector<pair<int, int>> directions{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (auto dir : directions) {
int newX = current.x + dir.first;
int newY = current.y + dir.second;
if (newX >= 0 && newX < grid.size() && newY >= 0 && newY < grid[0].size()
&& !closedSet[newX][newY] && !grid[newX][newY]) {
double tentative_g_score = current.g_cost + 1.0;
bool isBetter = false;
auto it = openSet;
while (!it.empty()) {
Node node = it.top(); it.pop();
if (node.x == newX && node.y == newY) {
if (tentative_g_score < node.g_cost) {
node.g_cost = tentative_g_score;
node.h_cost = heuristic(newX, newY, goal.first, goal.second);
node.parent = make_pair(current.x, current.y);
isBetter = true;
}
break;
}
}
if (!isBetter) {
openSet.push(Node{newX, newY, tentative_g_score,
heuristic(newX, newY, goal.first, goal.second),
make_pair(current.x, current.y)});
}
}
}
}
return {};
}
```
此代码实现了基本的 A* 算法逻辑,适用于二维网格地图上的路径规划问题。
---
#### 动态规划解决路径计数问题
对于某些特定场景下的路径规划问题(如计算从起点到终点的不同路径数量),可以采用动态规划的方法。假设我们有一个 m×n 大小的地图,则可以通过状态转移方程 `dp[i][j] = dp[i-1][j] + dp[i][j-1]` 来求解[^4]。
下面是具体的 C++ 实现:
```cpp
#include <iostream>
#include <vector>
using namespace std;
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i) dp[i][0] = 1;
for (int j = 0; j < n; ++j) dp[0][j] = 1;
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
int main() {
cout << "Unique Paths: " << uniquePaths(3, 7) << endl;
return 0;
}
```
上述程序展示了如何利用动态规划高效地解决问题,并且具有较低的时间复杂度 O(m*n)。
---
#### D* Lite 算法简介
除了经典的 A* 和动态规划外,还有更高级别的在线重规划算法——D* Lite。该算法由 Koenig S 和 Likhachev M 提出,适合于环境动态变化的情况。其核心思想是在未知环境中逐步构建一致性和最佳性[^2]。
由于篇幅限制,这里仅提供理论框架而不展开具体代码实现。
---