C++ 实现JPS(完整代码)

这篇博客详细介绍了如何使用C++实现JPS(Jump Point Search)算法,包括JpsMap.h和JpsMap.cpp两个关键文件的代码实现,以及地图数据结构的定义。JPS是一种优化的A*搜索算法,特别适用于网格地图,能显著减少计算量,提高路径搜索效率。
摘要由CSDN通过智能技术生成

 JpsMap.h

#pragma once
#include "MapManager.h"
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <list>
#include <vector>
#include <set>
#include <unistd.h>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <memory>
#include <functional>

class Node
{
    public:
    NodePtr parent;
    int32 x;
    int32 y;
    int32 gCost;
    int32 hCost;
    int32 dir;
    std::set<int32> forceL;

    Node( int32 _x, int32 _y )
    {
        parent = nullptr;
        x      = _x;
        y      = _y;
        gCost  = 0;
        hCost  = 0;
        dir    = -1;
        forceL = std::set<int32>();
    }

    ~Node(){}
};

namespace jps
{
    const int32 DirX[8] = { 1,  1,  0, -1, -1, -1,  0,  1 };
    const int32 DirY[8] = { 0,  1,  1,  1,  0, -1, -1, -1 };

    
    class JpsFind final
    {
        NodePtr startNode_;
        NodePtr endNode_;
        jpsmap::MapMgr map_mgr_;
        bool jpsN_;

        std::list<NodePtr> openL_;
        std::unordered_set<int32> closeL_;
        std::map<int32, NodePtr> nodeL_;

        public:
        void CalcPath();
        void AddToOpenL( const NodePtr& node );
        NodePtr FindByOpenL( const NodePtr& node );
        bool    FindByCloseL( const NodePtr& node );
        int32   GetDistance( int32 s_x, int32 s_y, int32 e_x, int32 e_y );
        std::vector<NodePtr> GetNeighbours( const NodePtr& node );
        std::vector<NodePtr> GetSuccessJumpNodeL( const NodePtr& node );
        void Init();
        void GetPath( int32 s_x, int32 s_y, int32 e_x, int32 e_y );
        NodePtr Jump( const NodePtr& node, int32 dir );
        NodePtr JumpInclined( const NodePtr& node, int32 dir );
        NodePtr JumpStr( const NodePtr& node, int32 dir );
        void FillMap()const;
        bool Walk( int32 x, int32 y )const;
        NodePtr GetNode( int32 x, int32 y );
        std::vector<NodePtr> GetForceNodeL( const NodePtr& node );
        std::vector<NodePtr> GetWholeJumpNodePath();
    };
}

JpsMap.cpp

#include "JpsMap.h"

namespace jps
{
    int32 JpsFind::GetDistance( int32 s_x, int32 s_y, int32 e_x, int32 e_y )
    {
        return std::sqrt(std::abs(e_x-s_x)*std::abs(e_x-s_x) + std::abs(e_y-s_y)*std::abs(e_y-s_y));
    }

    void JpsFind::AddToOpenL( const NodePtr& node )
    {
        if( !Walk(node->x, node->y) )
            return;

        if( FindByCloseL(node) )
            return;

        openL_.push_back(node);

        openL_.sort([](const NodePtr& l, const NodePtr& r)
            {
                return (l->gCost + l->hCost) < (r->gCost + r->hCost);
            });

    }

    NodePtr JpsFind::FindByOpenL( const NodePtr& node )
    {
        auto it = std::find_if(openL_.begin(), openL_.end(), [&node](const NodePtr& l)
            {
                return node->x == l->x && node->y == l->y;
            });
        
        bool is_open = false;

        if( it != openL_.end() )
            return *it;

        return NodePtr();
    }

    bool JpsFind::FindByCloseL( const NodePtr& node )
    {
        auto it = closeL_.find((node->x<<16) | node->y);
        if( it != closeL_.end() )
            return true;

        return false;
    }

    bool JpsFind::Walk( int32 x, int32 y )const
    {
        auto pos = map_mgr_.Find(x, y);
        if( pos && pos->walk )
            return true;

        return  false;
    }

    void JpsFind::FillMap()const
    {
        map_mgr_.FillMap();
    }

    NodePtr JpsFind::GetNode( int32 x, int32 y )
    {
        int32 key = (x<<16) | y;

        auto it = nodeL_.find(key);
        if( it != nodeL_.end() )
            return it->second;

        NodePtr node = NodePtr(new Node(x, y));
        if( !node )
            NodePtr();

        nodeL_[key] = node;

        return node;
    }

    void JpsFind::CalcPath()
    {
        while( !openL_.empty() )
        {
 
当然,我可以为您提供JPS算法的C代码示例。下面是一个简单的实现: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ROWS 10 #define COLS 10 typedef struct { int x; int y; } Point; typedef struct { int f; int g; int h; Point parent; Point current; } Node; int heuristic(Point start, Point end) { return abs(end.x - start.x) + abs(end.y - start.y); } bool isValid(int x, int y) { return (x >= 0 && x < ROWS && y >= 0 && y < COLS); } bool isBlocked(int grid[ROWS][COLS], int x, int y) { return (grid[x][y] == 1); } bool isDestination(Point current, Point end) { return (current.x == end.x && current.y == end.y); } void tracePath(Node node, int grid[ROWS][COLS]) { printf("Path: \n"); while (!(node.parent.x == node.current.x && node.parent.y == node.current.y)) { printf("(%d, %d)\n", node.current.x, node.current.y); Point temp = node.parent; node.parent = node.parent.parent; node.current = temp; } printf("(%d, %d)\n", node.current.x, node.current.y); } void jumpPointSearch(int grid[ROWS][COLS], Point start, Point end) { if (!isValid(start.x, start.y) || !isValid(end.x, end.y)) { printf("Invalid start or end point.\n"); return; } if (isBlocked(grid, start.x, start.y) || isBlocked(grid, end.x, end.y)) { printf("Start or end point is blocked.\n"); return; } bool closedList[ROWS][COLS]; memset(closedList, false, sizeof(closedList)); Node node; node.f = 0; node.g = 0; node.h = 0; node.parent = start; node.current = start; int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; while (!isDestination(node.current, end)) { int x = node.current.x; int y = node.current.y; closedList[x][y] = true; for (int i = 0; i < 8; i++) { int newX = x + dx[i]; int newY = y + dy[i]; if (isValid(newX, newY) && !closedList[newX][newY] && !isBlocked(grid, newX, newY)) { int gNew = node.g + abs(dx[i]) + abs(dy[i]); int hNew = heuristic({newX, newY}, end); int fNew = gNew + hNew; if (grid[newX][newY] == 0 || fNew < grid[newX][newY]) { grid[newX][newY] = fNew; Node newNode; newNode.f = fNew; newNode.g = gNew; newNode.h = hNew; newNode.parent = node.current; newNode.current = {newX, newY}; node = newNode; } } } } tracePath(node, grid); } int main() { int grid[ROWS][COLS] = { {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 1, 1, 1, 1, 1, 1}, {0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 1, 1, 1, 1, 0, 1}, {0, 0, 1, 1, 1, 1, 1, 1, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 1, 1, 1}, {0, 0, 1 ,1 ,1 ,1 ,1 ,1 ,1 ,1} }; Point start = {0 ,2}; Point end = {9 ,9}; jumpPointSearch(grid, start ,end); return 0; } ``` 这是一个简单的网格地图上使用JPS算法寻找路径的示例。请注意,这个示例是针对一个10x10的网格地图,您可以根据需要进行适当的调整。在代码中,0表示可通过的区域,1表示阻塞的区域。算法将打印出从起点到终点的路径坐标。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值