代码编织梦想

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define null 0
#define true 1
#define false 0

typedef unsigned int bool;

#define TRED "\033[31m"  //Red
#define TGRE "\033[32m"  //Green
#define TYEL "\033[33m"  //Yellow
#define TBLU "\033[34m"  //Blue
#define TPUR "\033[35m"  //Pur
#define TCYA "\033[36m"  //Dark green
#define CLER "\033[0m"

#define PRINT_COLOR(COLOR) printf("%s O ", COLOR)

#define MAP_SIZE 32

#define CALC_H(x, y, targetX, targetY) (abs((x) - (targetX)) + abs((y) - (targetY)))

static char map[][33] = {
"oooooooooo*ooooooooooooooooooooo",
"oooooooooo*ooooooooooooooooooooo",
"ooooo*oooo*ooooooooooooooooooooo",
"ooooo*ooo*oooooooooooooooooooooo",
"ooooo*oX*ooooooooooooooooooooooo",
"ooooo***oooooooooooooooooooooooo",
"oooooooooooooooooooooooooooooooo",
"oooooooooooooooooooooooooo*ooooo",
"ooooooooooooooooooooooooo*oooooo",
"oooooooooooooooooooooooo*ooooooo",
"ooooooooooooooooooooooo*oooooooo",
"oooooooooooooooooooooo*ooooooooo",
"ooooooooooooooooooooo*oooooooooo",
"oooooooooooooooooooo*ooooooooooo",
"ooooooooooooooooooo*oooooooooooo",
"oooooooooooooooooo*ooooooooooooo",
"ooooooooooooooooo*oooooooooooooo",
"oooooooooooooooo*ooooooooooooooo",
"ooooooooooooooo*oooooooooooooooo",
"ooooooooooooo**ooooooooooooooooo",
"oooooooooo***ooooooooooooooooooo",
"ooooooooo*ooooooooooooooooooooo",
"oooooooo*ooooooooooooooooooooooo",
"ooooooo*oooooooooooooooooooooooo",
"oooooo*ooooooooooooooooooooooooo",
"ooooo*ooooooooooooooooo***oooooo",
"oooooooooooooooooooooo*oX*oooooo",
"oooooooooooooooooooooo*oo*oooooo",
"oooooooooooooooooooooo*oo*oooooo",
"oooooooooooooooooooooo*oo*oooooo",
"oooooooooooooooooooooo*oo*oooooo",
"oooooooooooooooooooooooooooooooo"
};

void printMap();

typedef struct _Node {
	int x, y;
	float f, g, h;
	struct _Node* parent;
	struct _Node* pnext;
} Node;

Node* createAndAddNode(Node* list, int x, int y, float g, float h, Node* parent);

Node* addNode(Node* list, Node* node);

Node* removeNode(Node* list, Node* node);

Node* searchMinF(Node* list);

Node* findNodeInList(Node* list, int x, int y);

bool isNaighbourReachable(int x, int y, int dx, int dy);

void astarSearch(int startx, int starty, int endx, int endy) {
	int x, y;
	float g;
	Node *openList = null, *closeList = null;
	Node *node, *existsNode, *resultNode = null;

	// 1st, Put start node into open list.
	openList = createAndAddNode(openList, startx, starty, 0, CALC_H(startx, starty, endx, endy), null);

	while (resultNode == null && openList != null) {
		// Search the node with minimum f value in the open list.
		node = searchMinF(openList);

		x = node->x;
		y = node->y;

		// search neighbour node 
		for (int i = -1; i <= 1 && resultNode == null; i++) {
			for (int j = -1; j <= 1; j++) {
				// Skip itself (0, 0).
				if (i == 0 && j == 0) {
					continue;
				}

                // Ignore the node when it is already in close list.
				if (null != findNodeInList(closeList, x + i, y + j)) {
					continue;
				}

                // Check if the naighbour node is reachable.
				if (isNaighbourReachable(x, y, i, j)) {

					// If the naighbour is the target node, just record current node and stop searching.
					if (x + i == endx && y + j == endy) {
					    resultNode = node;
						break;
				    }

                    // Calculate g value for this naighbour.
					g = (i * j == 0 ? 1 : 1.4) + node->g;

                    // If the naighbour is already exists in open list, we will try to update the parent and g/f value for it.
					if (null != (existsNode = findNodeInList(openList, x + i, y + j))) {
						// If the naigbour access from this node has a lower g, we can update the parent and g/f value for it.
				        if (existsNode->g > g) {
							existsNode->parent = node;
							existsNode->g = g;
							existsNode->f = existsNode->h;
						}
					} else {
						// Add new node into open list.
					    openList = createAndAddNode(openList, x + i, y + j, g, CALC_H(x + i, y + j, endx, endy), node);
					}	
				}
			}
		}

		// remove node from openlist
		openList = removeNode(openList, node);

		// add node into close list.
		closeList = addNode(closeList, node);
    }

    // mark result path on the map.
	while (resultNode != null) {
		map[resultNode->y][resultNode->x] = '#';
		resultNode = resultNode->parent;
	} 
}

int main() {
	astarSearch(7, 4, 24, 26);
	printMap();
	return 0;
}

void printMap() {
	for (int i = 0; i < 32; i++) {
		for (int j = 0; j < 32; j++) {
			if (map[i][j] == '*') {
				// Fence
				PRINT_COLOR(TYEL);
			} else if (map[i][j] == '#') {
				// path
				PRINT_COLOR(TGRE);
			} else if (map[i][j] == 'X') {
				// start/end
				PRINT_COLOR(TRED);
			} else {
				PRINT_COLOR(TPUR);
			}
		}
		printf("\n");
	}
}

Node* createAndAddNode(Node* list, int x, int y, float g, float h, Node* parent) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->x = x;
	newNode->y = y;
	newNode->g = g;
	newNode->h = h;
	newNode->f = g + h;
	newNode->pnext = list;
	newNode->parent = parent;
	return newNode;
}

Node* addNode(Node* list, Node* node) {
	node->pnext = list;
	return node;
}

Node* removeNode(Node* list, Node* node) {
	Node* nextNode, *currentNode;
	if (list == node) {
		return list->pnext;
	}

	currentNode = list;

	while ((nextNode = currentNode->pnext) != null) {
		if (nextNode == node) {
			// remove node.
			currentNode->pnext = nextNode->pnext;
			break;
		}
		currentNode = nextNode;
	}

	return list;
}

Node* searchMinF(Node* list) {
	float minf = list->f;
	Node* nodeWithMinF = list;

	while (list->pnext != null) {
		list = list->pnext;
		if (list->f < minf) {
			minf = list->f;
			nodeWithMinF = list;
		}
	}	

	return nodeWithMinF;
}

Node* findNodeInList(Node* list, int x, int y) {
	while (list != null) {
		if (list->x == x && list->y == y) {
			return list;
		}
		list = list->pnext;
	}

	return null;	
}

bool isNaighbourReachable(int x, int y, int dx, int dy) {
	int targetx = x + dx, targety = y + dy;
	char targetValue;
	if (dx * dy == 0) {
		if (targetx >= 0 && targetx < MAP_SIZE && targety >= 0 && targety < MAP_SIZE) {
			targetValue = map[targety][targetx];
			return  targetValue == 'o' || targetValue == 'X';
		} else {
			return false;
		}
	}

	if (targetx < 0 || targetx >= MAP_SIZE || targety < 0 || targety >= MAP_SIZE) {
		return false;
	}

	targetValue = map[targety][targetx];

	if (targetValue != 'o' && targetValue != 'X') {
		return false;
	}

	if (map[targety][x] == 'o') {
		return true;
	}

	if (map[y][targetx] == 'o') {
		return true;
	}

	return false;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/indianpapa/article/details/127992797

a-star(a*)寻路算法 c++ 自己实现的完整代码(小白) - 注释,输出都保留,欢迎讨论指出问题_小mamama哥的博客-爱代码爱编程

想做游戏,所以最近看了下算法,看到了常用的 A*算法 索性就学了一学,感觉unity3d上面好像有封装的寻路算法,还是想着去实现一下,大概就看了很多百度,一共用了两天,把它简单实现了下。 前提: 我很久没有用C++了,所

d*路径搜索算法原理解析及python实现-爱代码爱编程

D*路径搜索算法原理解析及Python实现 1.D*算法简述2.操作2.1扩张2.2障碍处理2.3 发生死锁 3.伪代码3.1扩张3.2Raise检查 4.变体Focussed D*D* Lite 5.最

【算法】图解a* 搜索算法_hua zhu的博客-爱代码爱编程_a*算法流程图

最新版本参考(含代码实现):【机器人】路径规划 - 图解A* 搜索算法 - 知乎 A* 算法是启发式搜索算法,是根据Dijkstra算法改进而来。 问题引入 如下图所示,S为起始(start)节点,G为目标(goal)节点。 节点之间连线是两点的路径长度,如A到E的路径长度c(A,E) = 9。节点旁的h值时当前节点到达目标节点(G)的预估值,如h

【规划】d*路径搜索算法及python实现_笑扬轩逸的博客-爱代码爱编程

文章源自CSDN博主 云水禅心_心一 D*路径搜索算法 “D_star算法”的名称源自 Dynamic A Star,最初由Anthony Stentz于“Optimal and Efficient Path Planning for Partially-Known Environments”中介绍。它是一种启发式的路径搜索算法,适合面对周围环境未知

python3趣味系列题7(续)-----a*算法获取迷宫最优路径-爱代码爱编程

前文:Python3 趣味系列题7 ------ Prim算法生成完美迷宫 一、A*算法 寻找路径的算法有很多,例如BFS算法、Dijkstra算法等。BFS算法可以在较短时间内寻找到从起点到结束点的路径,但不一定是最优

a-star(a*)算法-爱代码爱编程

【由于专栏后几篇限制vip观看,我又把完整算法在公众号“武汉AI算法研习”进行了发布,可以查看全专栏文章。】 A-Star(A*)算法作为Dijkstra算法的扩展,在寻路和图的遍历过程中具有一定的高效性。 【适用范围

Unity 算法 之 A星(A Star/A*)寻路算法实现和封装,并带动态演示Demo-爱代码爱编程

Unity 算法 之 A星(A Star/A*)寻路的算法法实现和封装,并带动态演示Demo   目录 Unity 算法 之 A星(A Star/A*)寻路的算法法实现和封装,并带动态演示Demo 一、简单介绍 二、 A星(A Star/A*)寻路算法相关知识 1、什么是A星(A Star/A*)寻路算法 2、寻路:寻找最短路径并避开障碍物

路径规划A-star算法详解-爱代码爱编程

路径规划A-star算法详解 原文来源于Amit’s A* Pages,译文参考CSDN博客。参考自游戏中的路径规划,放在现实世界中的机器人和无人车同样适用,文章结构和原文相同,但更多的是意译,新增和删减部分内容,使得文章读起来适合我的口味,作为参考笔记,同时共享给大家。 起源于游戏中的路径搜索

A*算法(A-star Algorithm)搜索最短路径(含C/C++语言实现代码)-爱代码爱编程

目录 基本介绍 基本原理 有关定义和变量介绍 具体搜索过程 结束条件 与Dijkstra算法的比较 实现代码 运行结果 参考文章 基本介绍   在我们的日常生活中由许多方面都会涉及到 “最短路径” 的搜索问题,比如ROS机器人中根据给定地图进行全局路径规划,或者游戏中NPC的移动计算,线上游戏的的BOT计算等。A*算法作为一种

A* 路径搜索算法介绍及完整代码-爱代码爱编程

1.简介 A* (A-Star) 算法一种静态路网中求解最短路径最有效的方法之一, 是一种常用的启发式算法. 启发式算法:通过启发函数(heruistic)计算出键值, 引导算法的搜索方向. 2. 算法描述 Ray Wenderlich - Introduction to A* Pathfinding 此文非常好的介绍了A*算法的逻辑及其中的关键

A_Star算法C++实现-爱代码爱编程

关于A星算法,在这里提两个关键点:    (1).适应用于静态场景。A星算法需要提前构建场景地图(SLAM),在此基础之上做路径规划。    (2).启发式搜索。采用位置评估手段计算路径代价。比如我们要用的曼哈顿距离。 #include <iostream> #include <algorithm> #include

A*搜索算法(A-Star Search)简介及保姆级代码解读-爱代码爱编程

A*搜索算法简介及保姆级代码解读 1. A*算法简单介绍1.1 A*算法理论基础1.1.1 节点计算1.1.2 由计算得出的小结论1.2 算法逻辑结构2. 代码解析2.1 引入地图2.2 预处理2.3 定义父节点`parent`2.4 主循环2.4.12.4.22.4.32.4.42.4.52.5 画图3. 结果 1. A*算法简单介绍 A*

3 移动机器人路径规划(4- A*路径规划算法)-爱代码爱编程

3 移动机器人路径规划 4.1 Astat路径规划算法原理4.2 Astat路径规划例子示例4.3 Astat路径规划算法MATLAB代码4.3.1 MATLAB代码示例4.3.2 主代码:Astat.m4.3.3 函数代码:Astat_NextNode.m4.4 Astat路径规划算法Python代码4.4.1 Python实现示例4.4.2 辅

路径规划之 a* 算法_a*算法路径规划-爱代码爱编程

参考链接 原文地址 https://github.com/paulQuei/a-star-algorithm?spm=a2c6h.12873639.0.0.21834662LgAvMP 路径规划之 A* 算法 算法介绍

【路径规划】全局路径规划算法——a*算法(含python实现 | c++实现)_a*算法路径规划-爱代码爱编程

文章目录 参考资料1. 算法简介2. 算法精讲2.1 预处理2.2 开始搜索2.3 继续搜索2.4 确定实际路径 3. 算法总结3.1 算法步骤3.2 伪代码 4. python实现5. c++实现