精华内容
下载资源
问答
  • A星算法C++代码实现

    2014-10-13 10:43:50
    A星算法C++代码实现,可以实现从初始点到终点的最短路径规划,采用的是曼哈顿方法。
  • A星算法C++实现

    2017-09-23 16:46:01
    C++实现的A*寻路算法,经过测试,在有障碍物的情况下,路径为期望路径,内附测试结果,可以修改地图的大小及障碍物位置,比如大小改为1920*1080,使其更接近真实电脑屏幕或者手机屏幕分辨率,得到更为贴近实际的运算...
  • a星算法c++实现

    2014-04-08 14:08:40
    c++实现的a星算法,使用vs2010打开项目。
  • A星算法C++小样例

    热门讨论 2013-12-07 14:31:18
    一个A星寻路算法C++小样例,可参考我的博客
  • A星算法,A*算法C++实现,8数码求解,带MFC图形界面显示动态求解过程
  • 这是A*算法C++(MSVC)实现,利用了模板函数,有两个测试用例,一个是迷宫寻路,一个是求解八数码问题。 压缩包内包括: Astar.hpp //这是A*算法的模板函数实现 (还附送个快排算法 testMain_eightDigital.cpp //这...
  • A*算法A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法算法中的距离估算值与实际值越接近,最终搜索速度越快。c++语言实现
  • 代码中实现了3种寻路算法AStar,AStar_Direct,BStar() 在VS2019环境下运行,建议以release方式运行,DEBUG没有调会崩溃
  • A星解迷宫算法C++

    2016-10-21 17:09:40
    C++写的A星解迷宫算法,使用方法:先点封口按钮,再用鼠标在入口画线,再点最右边按钮,再迷宫内分别点击入口、出口位置 ,注意别点到墙,再点执行按钮。
  • 人工智能A星算法(C++).doc
  • A星寻路算法C++实现

    千次阅读 2017-09-23 16:49:32
    A*寻路算法C++实现,共两个文件 astar.h astar.cpp 代码如下 // astar.h BEGIN #ifndef ASTAR_H #define ASTAR_H #include #include #include // 地图格子数据结构 struct grid_t { int ...

    A*寻路算法的C++实现,共两个文件 astar.h astar.cpp

    代码如下

    // astar.h BEGIN

    #ifndef ASTAR_H
    #define ASTAR_H

    #include <stdio.h>
    #include <vector>
    #include <set>

    // 地图格子数据结构
    struct grid_t
    {
    int id; // grid id {1,100}
    int x; // x location {0,9}
    int y; // y location {0,9}
    int g; // g value 10{front, back, left, right}
    //14{east_north, east_south, west_east, west_south}
    int h; // h value ( diffx + diffy ) * 10
    int block; // if available
    int total; // total = g + h*10

    void show(){
    printf("{%d,%d,%d,%d,%d,%d,%d}, ", id, x, y, g, h, block, total);
    }
    };

    // 地图格子信息 规格 10*10
    extern std::vector<grid_t> map;

    // 地图初始化 大小: 10*10 以左下角(0,0)作为坐标起点
    void init_map();

    //经过的格子编号 因有先后顺序 因此需要用vector存储
    extern std::vector<int> path_grid_id;
    grid_t* find(int x, int y);
    #endif


    // astar.h END



    // astar.cpp BEGIN

    #include <sys/time.h>
    #include <stdlib.h>
    #include <time.h>
    #include "astar.h"

    std::vector<grid_t> map;
    std::vector<int> path_grid_id;


    int start = 12;
    int end = 78;

    int abs(int val)
    {
    if( val >= 0 ){
    return val;
    }
    return -val;
    }

    //grid_t* end_grid = &map[end-1];
    grid_t* end_grid = NULL;
    void init_map()
    {
    map.clear();
    for( int i=1; i<=100; i++ ){
    grid_t grid;
    grid.id = i;
    grid.x = (i-1) % 10;
    grid.y = (i - 1) / 10;
    grid.h = 0;
    grid.g = 0;
    grid.block = 0;
    grid.total = 0;

    // 设置障碍
    if( grid.x==4 && grid.y >= 3 && grid.y <= 6 ){
    grid.block = 1;
    }

    map.push_back(grid);
    }
    }

    struct id_val_t
    {
    int id;
    int val;
    };

    grid_t* find(int x, int y)
    {
    if( x < 0 || x > 9 ){
    return NULL;
    }
    if( y < 0 || y > 9 ){
    return NULL;
    }
    int id = y*10 + (x+1);
    if( id <= 0 || id > map.size() ){
    return NULL;
    }
    return &map[id-1];
    }

    grid_t* find_next_grid(int id)
    {
    if( id <= 0 || id > 100 ){
    return NULL;
    }
    grid_t* grid = &map[id-1];

    // 8 grid around
    //path_grid_id.push_back(id);

    // all ready find the path
    for( int i=0; i<path_grid_id.size(); i++ ){
    if( path_grid_id[i] != end ){
    continue;
    }
    return NULL;
    }

    std::vector<id_val_t> vec_id_val;

    int loc[][2] = {
    {grid->x+1, grid->y},
    {grid->x-1, grid->y},
    {grid->x, grid->y+1},
    {grid->x, grid->y-1},
    {grid->x+1, grid->y+1},
    {grid->x+1, grid->y-1},
    {grid->x-1, grid->y+1},
    {grid->x-1, grid->y-1}
    };

    for( int i=0; i<8; i++ ){
    int loc_x = loc[i][0];
    int loc_y = loc[i][1];
    grid_t* find_grid = find(loc_x, loc_y);
    if( NULL == find_grid ){
    continue;
    }
    if( find_grid->block == 1 ){
    continue;
    }
    }


    //front grid
    grid_t* find_grid = find(grid->x+1, grid->y);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 10;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //back grid
    find_grid = find(grid->x-1, grid->y);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 10;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //left grid
    find_grid = find(grid->x, grid->y+1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 10;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //right grid
    find_grid = find(grid->x, grid->y-1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 10;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //east-north
    find_grid = find(grid->x+1, grid->y+1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 14;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //east-south
    find_grid = find(grid->x+1, grid->y+1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 14;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }

    //west-north
    find_grid = find(grid->x-1, grid->y+1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 14;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }






    //west-south
    find_grid = find(grid->x-1, grid->y-1);
    if( NULL != find_grid && find_grid->block == 0 ){
    find_grid->g = 14;
    find_grid->h = abs(end_grid->y - find_grid->y) + abs(end_grid->x - find_grid->x);
    find_grid->total = find_grid->h * 10 + find_grid->g;
    id_val_t id_val;
    id_val.id = find_grid->id;
    id_val.val = find_grid->total;
    vec_id_val.push_back(id_val);
    }


    int tid = vec_id_val[0].id;
    int tval = vec_id_val[0].val;
    for( int i=0; i<vec_id_val.size(); i++ ){
    if( vec_id_val[i].val >= tval ){
    continue;
    }
    tid = vec_id_val[i].id;
    tval = vec_id_val[i].val;
    }

    return &map[tid-1];

    //
    }


    int main(int argc, char* argv[])
    {
    init_map();
    for( int i=0; i<100; i++ ){
    if( i % 5 == 0 ){
    printf("\n");
    }
    map[i].show();

    // set grid 74 block
    if( i>= 73 && i<= 75 ){
    map[i].block = 1;
    }

    }
    end_grid = &map[end-1];

    int round = 1;
    if( argc >= 2 ){
    round = atoi(argv[1]);
    }

    struct timeval tv1;
    struct timeval tv2;

    gettimeofday(&tv1, NULL);
    int time1 = time(NULL);
    for( int round_num = 0; round_num < round; round_num++ ){
    path_grid_id.clear();
    path_grid_id.push_back(start);
    int grid_id = start;
    while(true){
    grid_t* grid = find_next_grid(grid_id);
    if( NULL == grid ){
    break;
    }
    path_grid_id.push_back(grid->id);

    bool is_over = false;
    for( int i=0; i<path_grid_id.size(); i++ ){
    if(path_grid_id[i] != end_grid->id){
    continue;
    }
    is_over = true;
    break;
    }

    if( is_over ){
    break;
    }

    grid_id = grid->id;
    }
    }
    int time2 = time(NULL);

    gettimeofday(&tv2, NULL);

    printf("path_grid_id size: %d\n", path_grid_id.size());
    for( int i=0; i<path_grid_id.size(); i++ ){
    printf("grid_id:%d\n", path_grid_id[i]);
    }
    printf("round:%d, use:%d\n", round, time2-time1);
    printf("tv1_sec:%d, tv1_usec:%d\n", tv1.tv_sec, tv1.tv_usec);
    printf("tv2_sec:%d, tv2_usec:%d\n", tv2.tv_sec, tv2.tv_usec);
    return 0;
    }

    // astar.cpp END


    编译

    $ g++ -g -o hello astar.cpp


    测试

    $ ./hello  [round]


    精确到微秒, 测试结果:
    执行1次
    round:1 use:0
    tv1_sec:1506153329, tv1_usec:920483
    tv2_sec:1506153329, tv2_usec:920516
    时间为920516-920483=33微秒

    执行1W次
    round:10000, use:0
    tv1_sec:1506154327, tv1_usec:283682
    tv2_sec:1506154327, tv2_usec:770319

    执行10W次
    round:100000, use:2
    tv1_sec:1506154390, tv1_usec:71109
    tv2_sec:1506154392, tv2_usec:666823

    round:1000000, use:25
    tv1_sec:1506154417, tv1_usec:960088
    tv2_sec:1506154442, tv2_usec:562360



    展开全文
  • B星算法C++版.rar

    2019-05-31 18:53:07
    b星寻路算法,里面也有a星算法,找了很久才找到的,有DEMO演示,供大家学习使用。b星寻路算法,里面也有a星算法,找了很久才找到的,有DEMO演示,供大家学习使用。
  • A星寻路算法C++源码

    2020-07-21 03:36:17
    #pragma once #include"pch.h" #include <vector> using namespace std; struct LatticeInfo { int m_X, m_Y, m_H;... LatticeInfo() :m_X(NULL), m_Y(NULL), m_G(NULL), m_F(NULL), m_H(NULL), m_Move(FAL
    #pragma once
    #include"pch.h"
    #include <vector>
    using namespace std;
    
    struct LatticeInfo
    {
    	int m_X, m_Y, m_H;
    	float m_G, m_F;
    	BOOL m_Move;
    	LatticeInfo* m_LatticeObj;
    	LatticeInfo() :m_X(NULL), m_Y(NULL), m_G(NULL), m_F(NULL), m_H(NULL), m_Move(FALSE), m_LatticeObj(nullptr) {};
    	LatticeInfo(int x, int y, BOOL m) :m_X(x), m_Y(y), m_G(NULL), m_F(NULL), m_H(NULL), m_Move(m), m_LatticeObj(nullptr) {};
    	bool operator==(const LatticeInfo* Info)
    	{
    		return (m_X == Info->m_X && m_Y == Info->m_Y) ? TRUE : FALSE;
    	}
    };
    
    class Astar
    {
    public:
    	Astar();
    	~Astar();
    	static bool comp(const LatticeInfo* _Left, const LatticeInfo* _Right);
    	void InitMapInfo(LPVOID** array, int width, int high);
    	vector<LatticeInfo*> FindMapNodes(int sx, int sy, int ex, int ey);
    	void FindMapNodeInfo(int Px, int Py, float Pg, LatticeInfo* FatherNode, LatticeInfo* EndPos);
    private:
    	int m_MapW, m_MapH;
    	LatticeInfo** m_LatticeInfo;
    	vector<LatticeInfo*> m_OpenVector;
    	vector<LatticeInfo*> m_CloseVector;
    };
    #include "pch.h"
    #include "Astar.h"
    #include<algorithm>
    
    
    Astar::Astar()
    	:m_LatticeInfo(nullptr)
    	, m_MapW(NULL)
    	, m_MapH(NULL)
    {
    }
    
    Astar::~Astar()
    {
    	for (auto i = 0; i < this->m_MapW; i++)
    	{
    		delete[] this->m_LatticeInfo[i];
    	}
    	delete[] this->m_LatticeInfo;
    }
    
    bool Astar::comp(const LatticeInfo* _Left, const LatticeInfo* _Right)
    {
    	return _Left->m_F > _Right->m_F; /*由大到小排序*/
    };
    
    void Astar::InitMapInfo(LPVOID** array, int width, int high)
    {
    	if (array == nullptr || width == NULL || high == NULL)
    	{
    		return;
    	}
    	this->m_MapW = width;
    	this->m_MapH = high;
    	this->m_LatticeInfo = new LatticeInfo * [width];
    	for (auto i = 0; i < width; i++)
    	{
    		this->m_LatticeInfo[i] = new LatticeInfo[high];
    	}
    	for (auto i = 0; i < width; i++)
    	{
    		for (auto n = 0; n < high; n++)
    		{
    			if ((int**)array[i][n] == 0)
    			{
    				this->m_LatticeInfo[i][n] = LatticeInfo(i, n, TRUE);
    				continue;
    			}
    			this->m_LatticeInfo[i][n] = LatticeInfo(i, n, FALSE);
    		}
    	}
    
    }
    
    vector<LatticeInfo*> Astar::FindMapNodes(int sx, int sy, int ex, int ey)
    {
    	//判断起点终点是否合法
    	if (sx < 0 || sx >= this->m_MapW || sy < 0 || sy >= this->m_MapW ||
    		ex < 0 || ex >= this->m_MapH || ey < 0 || ey >= this->m_MapH)
    	{
    		return vector<LatticeInfo*>();
    	}
    	//清空开启列表与关闭列表
    	this->m_OpenVector.clear();
    	this->m_CloseVector.clear();
    	//加入关闭列表
    	LatticeInfo* StartNode = &this->m_LatticeInfo[sx][sy];
    	LatticeInfo* EndNode = &this->m_LatticeInfo[ex][ey];
    	StartNode->m_LatticeObj = nullptr;
    	StartNode->m_F = NULL;
    	StartNode->m_G = NULL;
    	StartNode->m_H = NULL;
    	this->m_CloseVector.push_back(StartNode);
    	while (true)
    	{
    		//左上 x - 1 , y - 1
    		FindMapNodeInfo(StartNode->m_X - 1, StartNode->m_Y - 1, 1.4f, StartNode, EndNode);
    		//上 x , y - 1
    		FindMapNodeInfo(StartNode->m_X, StartNode->m_Y - 1, 1, StartNode, EndNode);
    		//右上x + 1 , y - 1
    		FindMapNodeInfo(StartNode->m_X + 1, StartNode->m_Y - 1, 1.4f, StartNode, EndNode);
    		//左 x - 1 , y
    		FindMapNodeInfo(StartNode->m_X - 1, StartNode->m_Y, 1, StartNode, EndNode);
    		//右 x + 1 , y
    		FindMapNodeInfo(StartNode->m_X + 1, StartNode->m_Y, 1, StartNode, EndNode);
    		//左下 x - 1 , y + 1
    		FindMapNodeInfo(StartNode->m_X - 1, StartNode->m_Y + 1, 1.4f, StartNode, EndNode);
    		//下 x , y + 1
    		FindMapNodeInfo(StartNode->m_X, StartNode->m_Y + 1, 1, StartNode, EndNode);
    		//右下 x + 1 , y + 1
    		FindMapNodeInfo(StartNode->m_X + 1, StartNode->m_Y + 1, 1.4f, StartNode, EndNode);
    		//如果开启列表为空表示没有可通行的路
    		if (this->m_OpenVector.empty())
    		{
    			return vector<LatticeInfo*>();
    		}
    		//开启列表排序(由大到小)
    		sort(this->m_OpenVector.begin(), this->m_OpenVector.end(), comp);
    		//最后的元素最小加入关闭列表
    		this->m_CloseVector.push_back(this->m_OpenVector.back());
    		//从新设置新的起点
    		StartNode = this->m_OpenVector.back();
    		//出栈最后的元素,也就是移除最小元素
    		this->m_OpenVector.pop_back();
    		if (StartNode == EndNode)
    		{
    			vector<LatticeInfo*> MapPath;
    			MapPath.push_back(EndNode);
    			while (EndNode->m_LatticeObj != nullptr)
    			{
    				MapPath.push_back(EndNode->m_LatticeObj);
    				EndNode = EndNode->m_LatticeObj;
    			}
    			reverse(MapPath.begin(), MapPath.end());
    			return MapPath;
    		}
    	}
    	return vector<LatticeInfo*>();
    }
    
    void Astar::FindMapNodeInfo(int Px, int Py, float Pg, LatticeInfo* FatherNode, LatticeInfo* EndNode)
    {
    	if (Px < 0 || Px >= this->m_MapW || Py < 0 || Py >= this->m_MapH)
    	{
    		return;
    	}
    	LatticeInfo* Node = &this->m_LatticeInfo[Px][Py];
    	if (!Node->m_Move)
    	{
    		return;
    	}
    	if (!this->m_OpenVector.empty())
    	{
    		for (size_t ite = 0; ite < this->m_OpenVector.size(); ite++)
    		{
    			if (this->m_OpenVector.at(ite) == Node)
    			{
    				return;
    			}
    		}
    	}
    	if (!this->m_CloseVector.empty())
    	{
    		for (size_t ite = 0; ite < this->m_CloseVector.size(); ite++)
    		{
    			if (this->m_CloseVector.at(ite) == Node)
    			{
    				return;
    			}
    		}
    	}
    	//f = g + h
    	Node->m_LatticeObj = FatherNode;	//保存父对象
    	Node->m_G = FatherNode->m_G + Pg;	//计算G值
    	Node->m_H = abs(EndNode->m_X - Node->m_X) + abs(EndNode->m_Y - Node->m_Y);
    	Node->m_F = Node->m_G + Node->m_H;
    	this->m_OpenVector.push_back(Node);
    }
    展开全文
  • A星算法自动寻路(C++源代码)

    热门讨论 2010-06-20 13:46:40
    A星算法自动寻路(C++源代码),供大家学习使用
  • C++ A星算法

    千次阅读 2015-10-23 18:20:15
    //查找算法 openList.push_back(start); Node* root = NULL; int find = 0; while(openList.size()>0){ root = openList[0]; if(root->x==end->x&&root->y==end->y){ find = 1; break; } //上下...
    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <algorithm>
    using namespace std;
    class Node{
    public:
    	int x;
    	int y;
    	int f;//g+h
    	int g;//起点到当前点的消耗 
    	int h;//重点到当前点的理论消耗 
    	Node* parent;
    	Node(int x,int y){
    		this->x = x;
    		this->y = y;
    		this->f = 0;
    		this->g = 0;
    		this->h = 0;
    		this->parent = NULL;
    	}
    	Node(int x,int y,Node* parent){
    		this->x = x;
    		this->y = y;
    		this->f = 0;
    		this->g = 0;
    		this->h = 0;
    		this->parent = parent;
    	}
    };
    
    //0表示可通过1表示障碍物 
    int map[101][101] = {
    		{0,0,0,1,0,1,0,0,0},
    		{0,0,0,1,0,1,0,0,0},
    		{0,0,0,0,0,1,0,0,0},
    		{0,0,0,1,0,1,0,1,0},
    		{0,0,0,1,0,1,0,1,0},
    		{0,0,0,1,0,0,0,1,0},
    		{0,0,0,1,0,0,0,1,0}
    	};
    vector<Node*> openList;
    vector<Node*> closeList;	
    int row;//地图总行数 
    int col;//地图总列数 
    Node* startPos;//开始点 
    Node* endPos;//结束点 
    int weightW;//平移权重 
    int weightWH;//斜移权重 
    
    int countH(Node* sNode,Node* eNode){
    	int w = abs(sNode->x - eNode->x);
    	int h = abs(sNode->y - eNode->y);
    	int cost = min(w,h)*weightWH + abs(w-h)*weightW;
    	return cost;
    }
    void countFGH(Node* sNode,Node* eNode,int cost){
    	int h = countH(sNode,eNode);
    	int g = sNode->parent->g + cost;
    	int f = h + g;
    	sNode->f = f;
    	sNode->h = h;
    	sNode->g = g;
    }
    //检测列表是否包含指定节点
     int isContains(vector<Node*>* v,int x,int y){
     	for(int i=0;i<v->size();i++){
    		if(v->at(i)->x==x&&v->at(i)->y==y){
    			return i;
    		}
        }
        return -1;
     }
    void initMap(){	
    	row = 7;
    	col = 9;
    	weightW = 10;
    	weightWH = 15;
    	startPos = new Node(5,1);
    	endPos = new Node(3,8);
    }
    
    void printMap(){
    	for(int i=0;i<row;i++){
    		for(int j=0;j<col;j++){
    			printf("%d ",map[i][j]);
    		}
    		printf("\n");
    	}
    }
    bool compare(Node* n1,Node* n2){
    	return n1->f <= n2->f;
    }
    void checkMove(int x,int y,Node* parent,Node* end,int cost){
    	if(map[x][y]==1){
    		return;
    	}
    	if(isContains(&closeList,x,y)!=-1){
    		return;
    	}
    	int index = -1;
    	if((index = isContains(&openList,x,y))!=-1){
    		//是否存在更小的G值
    	    Node* sNode = openList[index];
    	    if(parent->g+cost < sNode->g){
        		sNode->g = parent->g+cost;
        		sNode->f = sNode->g + sNode->h;
        	}
    	}else{
    		Node* n = new Node(x,y,parent);
    		countFGH(n,end,cost);
    		openList.push_back(n);
    	}
    }
    void printPath(Node* node){
    	if(node->parent != NULL){
    		printPath(node->parent);
    	}
    	//将走过的点标记为2 
    	//map[node->x][node->y] = 2;
    	printf("->%d,%d",node->x,node->y);
    }
    void releaseNode(Node* n){
    	if(n->parent != NULL){
    		releaseNode(n->parent);
    	}
    	delete n;
    }
    //-1错误0没找到1找到 
    int startSearch(Node* start,Node* end){
    	if(start->x<0||start->y<0||start->x>=row||start->y>=col
    	 ||end->x<0||end->y<0||end->x>=row||end->y>=col){
    		return -1;
    	}
    	if(map[start->x][start->y]==1||map[end->x][end->y]==1){
    		return -1;
    	}
    	
    	start->h = countH(start,end);
    	start->f = start->h + start->g;
    	//查找算法
    	openList.push_back(start);
    	Node* root = NULL;
    	int find = 0;
    	while(openList.size()>0){
    		root = openList[0];
    		if(root->x==end->x&&root->y==end->y){
    			find = 1;
    			break;
    		}	
    		//上下左右 
    		if(root->x > 0){
    			checkMove(root->x-1,root->y,root,end,weightW);
    		}
    		if(root->y > 0){
    			checkMove(root->x,root->y-1,root,end,weightW);
    		}
    		if(root->x < row-1){
    			checkMove(root->x+1,root->y,root,end,weightW);
    		}
    		if(root->y < col-1){
    			checkMove(root->x,root->y+1,root,end,weightW);
    		}
    		if(root->x < row-1&&root->y>0){
    			checkMove(root->x+1,root->y-1,root,end,weightW);
    		}
    		if(root->y < col-1&&root->x>0){
    			checkMove(root->x-1,root->y+1,root,end,weightW);
    		}
    		if(root->x>0&&root->y>0){
    			checkMove(root->x-1,root->y-1,root,end,weightW);
    		}
    		if(root->y<col-1&&root->x<row-1){
    			checkMove(root->x+1,root->y+1,root,end,weightW);
    		}
    		closeList.push_back(root);
    		openList.erase(openList.begin());
    		sort(openList.begin(),openList.end(),compare);
    	}
    	if(find){
    		printPath(root);
    		printf("\n");
    		//printMap();
    	}
    	releaseNode(root);
    	openList.clear();
    	closeList.clear();
    	return find;
    }
    
    
    int main(int argc, char *argv[]){
    	initMap();	
    	printMap();
    	int t = startSearch(startPos,endPos);
    	if(t==1){
    		printf("find!");
    	}else if(t==0){
    		printf("no find.");
    	}else{
    		printf("error!");
    	}
    	return 0;
    }

    展开全文
  • 九宫格 八数码问题完美解决方案, C++ A星算法 亲自调试完美无误版
  • C++ A星寻路算法

    热门讨论 2011-03-26 18:05:43
    使用C++实现了A星算法,加二叉堆优化,菜鸟新做,高手飘过,仅供新手学习
  • c++A星算法(附源码+资源)+ppt讲解。
  • 分享一下A星算法及A星优化算法源码,提供大家学习使用!
  • 带权重的A*算法C++、CMake实现
  • C++实现A星算法自动寻路

    千次阅读 2013-12-13 18:28:41
    虽然网上已经有许多A星算法的实现,但大多过于繁琐。这是实现的精简版,必须要吐槽一下C++对象的指针和引用,还有那让人蛋疼的while和递归居然是两码事(这bug调了我一晚上)。不行你试试,把下面的start()的方法改...

    虽然网上已经有许多A星算法的实现,但大多过于繁琐。这是实现的精简版,必须要吐槽一下C++对象的指针和引用,还有那让人蛋疼的while和递归居然是两码事(这bug调了我一晚上)。不行你试试,把下面的start()的方法改下成while() 注释可以参考http://blog.csdn.net/liucanlong/article/details/10334035

     

     

    #include "iostream"
    #include "set"
    #include "stdlib.h"
    #include "vector"
    #define N 1000
    using namespace std;
    class Node{
    	public:
    		int x;
    		int y;
    		int g;
    		int h;
    		int f;
    		Node *parent;
    		Node(int,int);
    		Node(){}
    		bool operator <(const Node &other)const{
    			if(this->f!=other.f){
    				return this->f<other.f;
    			}else {
    				return true;//允许相同 
    			}
    			
    		}
    }; 
    set<Node> full;
    set<Node> openL;
    set<Node> closeL;
    vector<Node> resultL;
    int map[N][N]={ 	{0,0,0,0,0,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,0,0,0,0},
    
    	                {0,0,0,0,1,0,0,0,0,0}};
    int col;
    int row;
    int line = 10;
    int xie = 14;
    
    int isContains(int x,int y,set<Node> *pL){
    	set<Node>::iterator iter = pL->begin();
    	for(int i=0;iter!=pL->end();iter++,i++){
    		if(iter->x==x&&iter->y==y)
    			return i;
    	}
    	return -1;
    }
    int isContains(int x,int y,vector<Node> *pL){
    	vector<Node>::iterator iter = pL->begin();
    	for(int i=0;iter!=pL->end();iter++,i++){
    		if(iter->x==x&&iter->y==y)
    			return 1;
    	}
    	return 0;
    }
    int countH(int x,int y,int endX,int endY){
    	int xSub = abs(x-endX);
        int ySub = abs(y-endY);
        int minXY = xSub>ySub?ySub:xSub;
        return minXY*xie+abs(xSub-ySub)*line;
    }
    
    void check(int x,int y,int cost,Node *parent,int endX,int endY){
    	if(x<0||x>=row||y<0||y>=col) return;
    	Node node(x,y);
    	node.parent = parent;
    	
    	node.g = parent->g+cost;
    	if(isContains(x,y,&closeL)!=-1) return;
    	if(map[x][y]==1){
    		closeL.insert(node);
    		return;
    	}
    	int index = isContains(x,y,&openL);
    	if(index!=-1){
    		set<Node>::iterator iter = openL.begin();
    		for(int i=0;i<index;i++)
    			iter++;
    		if(node.g<iter->g){
    			node.f = node.g+iter->h;
    			node.h = iter->h;
    			openL.erase(iter);
    			openL.insert(node);
    		}
    	}else{
    		node.h = countH(x,y,endX,endY);
    		node.f = node.h+node.g;
    		openL.insert(node);
    	}
    }
    void getPath(Node *node){
    	while(node->parent==NULL){
    		resultL.push_back(*node);
    		return;
    	}
    	getPath(node->parent);
    	resultL.push_back(*node);
    }
    
    int start(int endX,int endY){
    	if(openL.empty()) return 0;
    	set<Node>::iterator iter = openL.begin();
    	
    	Node n = *iter;
    	if(n.x==endX&&n.y==endY){
    		getPath(&n);
    		return 1;
    	}
    	check(n.x,n.y-1,line,&n,endX,endY); //左 
    	check(n.x,n.y+1,line,&n,endX,endY); //右 
    	check(n.x-1,n.y,line,&n,endX,endY); //上
    	check(n.x+1,n.y,line,&n,endX,endY); //下
    	check(n.x-1,n.y-1,xie,&n,endX,endY); //左上
    	check(n.x-1,n.y+1,xie,&n,endX,endY); //右上 
    	check(n.x+1,n.y-1,xie,&n,endX,endY); //左下
    	check(n.x+1,n.y+1,xie,&n,endX,endY); //右下 
    	closeL.insert(n);
    	openL.erase(iter);
    	return start(endX,endY);
    }
    
    int search(int startX,int startY,int endX,int endY){
    	if(map[startX][startY]==1||map[endX][endY]==1) return -1;
    	Node *root = new Node(startX,startY);
    	openL.insert(*root);
    	return start(endX,endY);
    }
    
    int main(){		
    	col = 10;
    	row = 6;
    	int flag = search(3, 0, 4, 7);
    	if(flag==0){
    		cout<<"no find!"<<endl;
    	}else if(flag==-1){
    		cout<<"format error"<<endl;
    	}else if(flag==1){
    		for(int i=0;i<row;i++){
    			for(int j=0;j<col;j++){
    				if(map[i][j]==1) cout<<"#";
    				else{
    					if(isContains(i,j,&resultL)){
    						cout<<"@";
    					}else{
    						cout<<" ";
    					}
    				}
    			}
    			cout<<endl;
    		}
    	}
    	system("pause");
    	return 0;
    }
    
    Node::Node(int x,int y){
    	this->x = x;
    	this->y = y;
    	this->g = 0;
    	this->h = 0;
    	this->f = 0;
    	this->parent = NULL;
    }
    


    展开全文
  • 自以为对于cocos2d了如指掌中,但从刚买的书中了解到了A星算法; 虽然书中只是一笔带过,连代码也没有但是经过我一个月的推敲,终于完成了A星算法。 ![在这里插入图片描述]...
  • #include<iostream> #include<deque> #include<algorithm> #include<iterator> using namespace std; #define M 3 class MatrixNode{ //定义 MatrixNode 类 public: int m; //在位个数 int d; //深度 int p;...
  • --- ---- #include<iostream> #include<deque> #include<algorithm> #include<iterator> using namespace std; #define M 3 class MatrixNode{ //定义MatrixNode类 public: int m; //在位个数 int d;...
  • c++游戏开发 AStar(A星)寻路(寻径)算法.zip

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 9,416
精华内容 3,766
关键字:

a星算法c++

c++ 订阅