A*寻路算法(8个方向)带debug

2 篇文章 0 订阅
1 篇文章 0 订阅

本垃圾的博客链接

思路可以参考这个https://blog.csdn.net/h348592532/article/details/44421753

如果有不对的请联系我(之前那个版本有点问题我改了一下)

寻路时:

紫色'.'表示在closeSet里的节点,红色'#'表示在openSet里的节点


寻路成功时:


下面那行对应键盘上的'QWEADZXC'

测试例子
20 
....................
....................
...##############...
................#...
................#...
................#...
................#...
................#...
................#...
s...............#...
................#...
................#...
...##############...
.................e..
....................
....................
....................
....................
....................
....................

#include <iostream>
#include <algorithm>
#include <list>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

const int maxn = 100;
struct node{
	int x, y;
	int f, g, h;
	char dir; 
	node(){
		f = 0;
		g = 0;
		h = 0;
	}
};
int dirx[8] = {-1, -1, -1,  0, 0,  1, 1, 1}; // 8个方向 
int diry[8] = {-1,  0,  1, -1, 1, -1, 0, 1};
int n, m;
string ans;
class aStart{
public:
	list<node> openSet;
	list<node> closeSet;
	char maze[maxn][maxn];
	node map[maxn][maxn], road[maxn][maxn], start, fin;
	
	
	void initMap();
	void drawMap();
	void debug();
	int getH(node a, node b);
	int getG(int i);
	void getDir(int i, node nei);
	bool isInclude(list<node> arr, node qur);
	void dfs(int x, int y);	
	void aStartSearch();		
};

void color(int x) // 给文字上色 
{
	if(x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);	
}

void gotoxy(int x,int y) // 将光标移动到指定坐标 
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut,pos);
}

void aStart::initMap(){ // 初始化地图 
	memset(map, 0, sizeof(map));
	memset(road, 0, sizeof(road));
	memset(maze, 0, sizeof(maze));
	cin >> n;
	m = n;
	for(int i = 0; i <= n+1; i++){
		maze[i][0] = '#';
		maze[0][i] = '#';
		maze[n+1][i] = '#';
		maze[i][n+1] = '#';
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> maze[i][j];
			if(maze[i][j] == 's'){
				start.x = i;
				start.y = j;
			}
			if(maze[i][j] == 'e'){
				fin.x = i;
				fin.y = j;
			}
			map[i][j].x = i;
			map[i][j].y = j;
		}
	}
}

void aStart::drawMap(){ // 打印地图 
	gotoxy(0, 0);
	for(int i = 0; i <= n+1; i++){
		for(int j = 0; j <= m+1; j++){
			if(maze[i][j] == '@'){
				color(4);
				cout << '@';
			}else if(maze[i][j] == '#'){
				color(7);
				cout << maze[i][j];
			}
			else{
				cout << ' ';
			}
		}
		cout << endl;
	}
}

void aStart::debug(){ // 打印openSet和closeSet中的点 
	list<node>::iterator it;
	for(it = openSet.begin(); it != openSet.end(); it++){
		gotoxy(it->y, it->x);
		color(4);
		cout << '*';
	}
	for(it = closeSet.begin(); it != closeSet.end(); it++){
		gotoxy(it->y, it->x);
		color(5);
		cout << '$';
	}
}

int aStart::getH(node a, node b){ // 计算当前点到终点的距离 h
	int h_dia = min(abs(a.x-b.x), abs(a.y-b.y));
	int h_str = abs(a.x-b.x) + abs(a.y-b.y);
	return (14 * h_dia + 10 * (h_str - 2*h_dia));
}

int aStart::getG(int i){ // 走下一步需要的花费 g 
	switch(i){
		case 0:
		case 2:
		case 5:
		case 7:return 14; // 走斜线 
		case 1:
		case 3:
		case 4:
		case 6:return 10; // 走直线 
	}
}

bool cmp(node a, node b){ // 对 list中的f继续排序  
	//if(a.f != b.f)
		return a.f < b.f;
	//return a.h < b.h;
}

bool aStart::isInclude(list<node> arr, node qur){ // 判断一个节点是否在list中 
	list<node>::iterator it;
	for(it = arr.begin(); it != arr.end(); it++){
		if(it->x == qur.x && it->y == qur.y){
			return true;
		}
	}
	return false;
}

void aStart::dfs(int x, int y){ // 回溯路径 
	if(x == start.x && y == start.y)
		return;
	else
		dfs(road[x][y].x, road[x][y].y);
	maze[x][y] = '@';
	ans += road[x][y].dir; 
}

void aStart::getDir(int i, node nei){
	switch(i){
		case 0: road[nei.x][nei.y].dir = 'Q'; break;
		case 1: road[nei.x][nei.y].dir = 'W'; break;
		case 2: road[nei.x][nei.y].dir = 'E'; break;
		case 3: road[nei.x][nei.y].dir = 'A'; break;
		case 4: road[nei.x][nei.y].dir = 'D'; break;
		case 5: road[nei.x][nei.y].dir = 'Z'; break;
		case 6: road[nei.x][nei.y].dir = 'X'; break;
		case 7: road[nei.x][nei.y].dir = 'C'; break;
	}
}

void aStart::aStartSearch(){
	int cnt = 0, cou = n, flag = 0;
	openSet.clear();
	closeSet.clear();
	openSet.push_front(start);
	while(!openSet.empty()){
		openSet.sort(cmp);
		node now = openSet.front();
		/*if(cnt >= 50){
			cou += n;
			cnt = 0;
		}
		gotoxy(cou+2, cnt++);
		cout << now.f << "--" << now.x << "--" << now.y << endl; 
		gotoxy(cou+2, cnt++);
		cout << "--------------------" << endl;
		*/
		if(now.x == fin.x && now.y == fin.y){ // 当终点在openSet中时结束循环 
			dfs(fin.x, fin.y);
			drawMap();
			cout << ans << endl;
			cout << "DONE" << endl;
			break;
		}
		openSet.pop_front();
		closeSet.push_back(now);
		for(int i = 0; i < 8; i++){
			flag = 0;
			node nei = map[now.x+dirx[i]][now.y+diry[i]];
			if(maze[now.x+dirx[i]][now.y+diry[i]] == '#' || now.x+dirx[i] >= n+1 || now.x+dirx[i] <= 0 || now.y+diry[i] >= m+1 || now.y+diry[i] <= 0)
		 		continue;
 			// 判断改节点是否在closeSet中 
		 	if(isInclude(closeSet, nei))
		 		continue;
	 		node tmp; // 临时节点,用来判断是否需要改变当前邻居的属性 
	 		tmp.g = now.g + getG(i); // 计算新的g值 
	 		//cout << now.g << endl;
	 		if(!isInclude(openSet, nei)){ // 如果不在openSet中,则加入并更新父节点 
	 			getDir(i, nei);
	 			road[nei.x][nei.y].x = now.x; 
				road[nei.x][nei.y].y = now.y;
	 			openSet.push_front(nei);
	 			flag = 1;
	 		}
		 	else if(tmp.g >= nei.g) // 如果当前节点的g比原来的g大,直接跳过 
		 		continue;
	 		// 如果比较小
	 		if(flag == 1) // 如果当前节点原来不在openSet中,且g更小 
		 		openSet.pop_front(); // 先把之前的节点删除
			// 更新父节点信息 
			getDir(i, nei);
			road[nei.x][nei.y].x = now.x; 
			road[nei.x][nei.y].y = now.y;
			// 更新改邻居节点的属性
		 	nei.g = tmp.g;
		 	nei.h = getH(nei, fin); 
		 	nei.f = nei.g + nei.h; // f = g + h;
		 	map[now.x+dirx[i]][now.y+diry[i]] = nei;
		 	openSet.push_front(nei); // 加入到openSet中 
 			//gotoxy(cou+2, cnt++);
		 	//cout << map[now.x+dirx[i]][now.y+diry[i]].f << '-' << map[now.x+dirx[i]][now.y+diry[i]].x << '-' << map[now.x+dirx[i]][now.y+diry[i]].y << endl;
		}
		//gotoxy(cou+2, cnt++);
		//cout << "--------------------" << endl;
		debug();
		Sleep(100);
	}
}

int main(){
	aStart t;
	while(1){
		t.initMap();
		t.drawMap();
		t.aStartSearch();
	}
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值