寻路算法(A*算法)实现(附C++代码)

–需求是求A到B的最短路径。这里的行走方案是上下左右,左上、左下、右上、右下八个方向(这个根据实际情况实际处理)。蓝色的块是障碍,不能行走。
在这里插入图片描述

步骤:
–维护待处理点的队列,初始只有起点即A。
–取出待处理点向各方向走。

如果到达点还没有点到达过,那么将处理点作为这个到达点的前点(用于找出路径),并设置到达点的距离为处理点+距离。
如果到达点已经有点到达过了,那么判断处理点的距离+两点距离是否小于到达点的距离,是的话将处理点设置为到达点的前点,并修改距离。
–结束条件:处理点队列内部没有数据点。

下面是C++代码

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Points
{
    int x;
    int y;
    Points(int xVal, int yVal):x(xVal), y(yVal){};
    string ToString()
    {
        return "(" + to_string(x) + "," + to_string(y) + ")";
    }
};

vector<Points> FindClosestPath(const vector<vector<bool>>& mat, const Points& start, const Points& end)
{
    queue<Points> inSerch;
    inSerch.push(start);
    
    ssize_t width = mat.size();
    ssize_t height = mat[0].size();
    vector<vector<pair<int, Points>>> datas;
    vector<pair<int, Points>> temp;
    for (int i = 0; i < height; i++)
    {
        temp.push_back(make_pair(-1, Points(0, 0)));
    }
    for (int i = 0; i < width; i++)
    {
        datas.push_back(temp);
    }
    
    datas[start.x][start.y].first = 0;
    
    auto bInMat = [width, height](Points point)->bool{
        return point.x >= 0 && point.y >= 0 && point.x < width && point.y < height;
    };
    
    // leftTop, left, leftBottom, Top, Bottom, RihgtTop, Right, RightBottom
    vector<Points> offsets = {Points(-1, -1), Points(-1, 0), Points(-1, 1), Points(0, -1), Points(0, 1), Points(1, -1), Points(1, 0), Points(1, 1)};
    vector<int> distances = {14, 10, 14, 10, 10, 14, 10, 14};
    
    while (inSerch.size() != 0) {
        ssize_t count = inSerch.size();
        for (int i = 0; i < count; i++)
        {
            Points point = inSerch.front();
            inSerch.pop();
            
            for (int j = 0; j < offsets.size(); j++)
            {
                Points dst = Points(point.x + offsets[j].x, point.y + offsets[j].y);
                if (bInMat(dst) && mat[dst.x][dst.y])
                {
                    if (datas[dst.x][dst.y].first == -1 || datas[dst.y][dst.y].first > datas[point.x][point.y].first + distances[j])
                    {
                        datas[dst.x][dst.y].first = datas[point.x][point.y].first + distances[j];
                        datas[dst.x][dst.y].second = point;
                        
                        if (dst.x != end.x || dst.y != end.y)
                        {
                            inSerch.push(dst);
                        }
                    }
                }
            }
        }
        
    }
    
    vector<Points> res;
    
    Points current = end;
    while (datas[current.x][current.y].first != -1)
    {
        res.push_back(current);
        current = datas[current.x][current.y].second;
        
        if (current.x == start.x && current.y == start.y)
        {
            res.push_back(current);
            break;
        }
    }
    
    reverse(res.begin(), res.end());
    return res;
}

int main(int argc, const char * argv[]) {
    
    vector<vector<bool>> mat = {
        {true, true, true, true, true},
        {true, true, true, true, true},
        {true, true, true, true, true},
        {true, false, false, false, false},
        {true, true, true, true, true},
        {true, true, true, true, true},
        {true, true, true, true, true}
    };
    
    vector<Points> res = FindClosestPath(mat, Points(1, 2), Points(4, 2));
    
    for (int i = 0; i < res.size(); i++)
    {
        cout << res[i].ToString();
        if (i != res.size() - 1)
        {
            cout << " -> ";
        }
    }
    
    if (res.empty())
    {
        cout << "No Way" << endl;
    }
    
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C语言实现的A*算法: ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #define ROW 10 #define COL 10 typedef struct Node { int x, y; int f, g, h; struct Node *parent; } Node; Node *openList[ROW * COL]; Node *closeList[ROW * COL]; int openLen = 0, closeLen = 0; int map[ROW][COL] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; int isObstacle(int x, int y) { if (x < 0 || x >= ROW || y < 0 || y >= COL) { return 1; } return map[x][y]; } int calcG(Node *node) { int g = 0; while (node->parent) { g += abs(node->x - node->parent->x) + abs(node->y - node->parent->y); node = node->parent; } return g; } int calcH(Node *node, Node *end) { return abs(node->x - end->x) + abs(node->y - end->y); } int calcF(Node *node, Node *end) { return calcG(node) + calcH(node, end); } void insertOpenList(Node *node) { int i; for (i = 0; i < openLen; i++) { if (openList[i]->f > node->f) { break; } } for (int j = openLen; j > i; j--) { openList[j] = openList[j - 1]; } openList[i] = node; openLen++; } Node *popOpenList() { Node *node = openList[0]; for (int i = 0; i < openLen - 1; i++) { openList[i] = openList[i + 1]; } openLen--; return node; } void insertCloseList(Node *node) { closeList[closeLen++] = node; } int isInCloseList(Node *node) { for (int i = 0; i < closeLen; i++) { if (closeList[i]->x == node->x && closeList[i]->y == node->y) { return 1; } } return 0; } void freeList(Node **list, int len) { for (int i = 0; i < len; i++) { free(list[i]); } } void printPath(Node *node) { while (node) { printf("(%d, %d) ", node->x, node->y); node = node->parent; } printf("\n"); } void aStar(int startX, int startY, int endX, int endY) { Node *start = (Node *)malloc(sizeof(Node)); start->x = startX; start->y = startY; start->f = 0; start->g = 0; start->h = 0; start->parent = NULL; insertOpenList(start); while (openLen > 0) { Node *cur = popOpenList(); if (cur->x == endX && cur->y == endY) { printPath(cur); freeList(openList, openLen); freeList(closeList, closeLen); return; } insertCloseList(cur); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int x = cur->x + i; int y = cur->y + j; if (isObstacle(x, y) || isInCloseList(&(Node){x, y})) { continue; } Node *node = (Node *)malloc(sizeof(Node)); node->x = x; node->y = y; node->parent = cur; node->g = calcG(node); node->h = calcH(node, &(Node){endX, endY}); node->f = calcF(node, &(Node){endX, endY}); insertOpenList(node); } } } printf("No path found.\n"); freeList(openList, openLen); freeList(closeList, closeLen); } int main() { map[1][1] = 1; map[2][1] = 1; map[3][1] = 1; map[4][1] = 1; map[5][1] = 1; map[6][1] = 1; map[7][1] = 1; map[8][1] = 1; map[9][1] = 1; aStar(0, 0, 9, 9); return 0; } ``` 这是一个简单的A*算法实现,可以在一个10x10的地图上找到起点到终点的最短径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值