c++ A*算法代码,(复制粘贴直接能跑)

具体原理网上很多,我就不赘述了。下面直接上代码,代码是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 &&current->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;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值