具体原理网上很多,我就不赘述了。下面直接上代码,代码是ai生成的,(用文心一言,回答一半直接结束了,后续继续也接不上,一句话就是难用。然后用通义千问,直接改了两行代码就能用了。文心一言小垃圾鉴定完毕。)
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
#define SWAMPCOST 2
struct Node {
int x, y;
int g; // 从起始节点到当前节点的实际代价
int h; // 启发式函数估算的代价,这里简单使用曼哈顿距离
Node* parent;
Node(int x_, int y_) : x(x_), y(y_), g(0), h(0), parent(nullptr) {}
int f() const { return g + h; } // 总代价函数
};
// 计算曼哈顿距离作为启发式函数
int heuristic(Node* a, Node* b) {
return abs(a->x - b->x) + abs(a->y - b->y);
}
// 实现优先队列,按f值排序
struct Compare {
bool operator()(Node* a, Node* b) {
return a->f() > b->f();
}
};
// A* 算法核心函数
vector<Node*> aStar(Node* start, Node* goal, vector<vector<int>>& grid) {
int width = grid.size();
int height = grid[0].size();
priority_queue<Node*, vector<Node*>, Compare> openSet;
vector<vector<Node*>> closedSet(width, vector<Node*>(height, nullptr));
start->g = 0;
start->h = heuristic(start, goal);
openSet.push(start);
while (!openSet.empty()) {
Node* current = openSet.top();
openSet.pop();
//printf("(%d,%d) ",current->x,current->y);
if (current->x == goal->x &¤t->y == goal->y)
break;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int movecost[]={1,999,SWAMPCOST,3};
for (int i = 0; i < 4; ++i) {
int newX = current->x + dx[i];
int newY = current->y + dy[i];
if (newX >= 0 && newX < width && newY >= 0 && newY < height && grid[newX][newY] != 1)
{
Node* neighbor = new Node(newX, newY);
//printf("(%d,%d)\n",newX,newY);
neighbor->g = current->g + movecost[grid[neighbor->x][neighbor->y]];
neighbor->h = heuristic(neighbor, goal);
neighbor->parent = current;
if (closedSet[newX][newY] == nullptr || neighbor->g < closedSet[newX][newY]->g) {
openSet.push(neighbor);
//printf("(%d,%d) ",neighbor->x,neighbor->y);
closedSet[newX][newY] = neighbor;
}
}
}
}
// 重构路径
vector<Node*> path;
Node* currentNode = closedSet[goal->x][goal->y];
while (currentNode != nullptr) {
path.push_back(currentNode);
currentNode = currentNode->parent;
}
reverse(path.begin(), path.end()); // 反转得到正确顺序的路径
return path;
}
int main() {
// 初始化一个简单的网格地图,0代表可通行,1代表障碍物,2代表沼泽耗费2移动点 ,3代表陷阱耗费3移动点
vector<vector<int>> grid = {
{0, 2, 0, 0},
{0, 3, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0}
};
Node* start = new Node(0, 0);
Node* goal = new Node(3, 0);
vector<Node*> path = aStar(start, goal, grid);
//vector<vector<char>> showPath={};
char showPath[4][4]={};
for (size_t i = 0; i < grid.size(); ++i) { // 遍历每一行
for (size_t j = 0; j < grid[i].size(); ++j) { // 遍历每一列
std::cout << grid[i][j] << " "; // 输出当前元素//
showPath[i][j]=grid[i][j]+'0';
}
std::cout << std::endl; // 换行,表示下一行开始
}
cout << "Path found:\n";
for (Node* node : path) {
cout << "(" << node->x << ", " << node->y << ") ";
showPath[node->x][node->y]='*';
}
std::cout << std::endl; // 换行,表示下一行开始
for (size_t i = 0; i < 4; ++i) { // 遍历每一行
for (size_t j = 0; j < 4; ++j) { // 遍历每一列
std::cout << showPath[i][j]; // 输出当前元素
if (j < 3) { // 如果不是最后一列,输出分隔符(例如空格)
std::cout << " ";
}
}
std::cout << std::endl; // 换行,表示下一行开始
}
cout << endl;
// 注意:实际应用中需要释放动态分配的内存
return 0;
}