A*寻路四方向版

#pragma once
#include <iostream>
#include <list>
#include <cmath>
#include <Windows.h>
using namespace std;
typedef struct dian
{
	int y;
	int x;
	struct dian* befront;
	int f;
	int g;//到起点
	int h;//到终点
}Dian;
class AXXL
{
private:
	
	list <Dian*> listdian;
	list <Dian*> shifang;
public:
	int StarX;
	int StarY;
	int EndX;
	int EndY;
	int X;
	int Y;
	int* map;
	AXXL(int sx, int sy, int ex, int ey, int x, int y, int* pp);
	void setmap();
	bool isCanMove(int l, int r);
	void xunlu();
	Dian* minF();
	void shuchu();
};
#include "AXXL.h"

AXXL::AXXL(int sx, int sy, int ex, int ey, int x, int y, int* pp)
{
	this->StarX = sx;
	this->StarY = sy;
	this->EndX = ex;
	this->EndY = ey;
	this->X = x;
	this->Y = y;
	this->map = pp;
}

void AXXL::setmap()
{
	system("cls");
	for (size_t i = 0; i < Y; i++)
	{
		for (size_t j = 0; j < X; j++)
		{
			cout << *(map + (i * X) + j);
		}
		cout << endl;
	}
	cout << endl;
}

bool AXXL::isCanMove(int x, int y)
{
	if (x<0||x>(this->X-1)||y<0||y>(this->Y-1))
	{
		return false;
	}
	if (*(map+(y*this->X)+x)==1|| *(map + (y * (this->X)) + x) == 2|| *(map + (y * this->X) + x) == 3)
	{
		return false;
	}
	return true;
}

void AXXL::xunlu()
{
	int t_x;
	int t_y;
	Dian* t = new Dian;
	t->x = this->StarX;
	t->y = this->StarY;
	t->befront = NULL;
	t->g = (abs(t->x-this->StarX)+abs(t->y-this->StarY));
	t->h= (abs(t->x - this->EndX) + abs(t->y - this->EndY));
	t->f = t->g + t->h;
	listdian.push_back(t);
	shifang.push_back(t);
	do
	{
		if (listdian.empty())
		{
			break;
		}
		t = minF();
		t_x = t->x;
		t_y = t->y;
		if (t_x == EndX && t_y == EndY)
		{
			break;
		}
		if (isCanMove(t_x-1,t_y))
		{
			*(map + (t_y * (this->X)) + t_x - 1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x - 1;
			nks->y = t_y;
			nks->befront = t;
			nks->g = (abs(t_x-1 - this->StarX) + abs(t_y - this->StarY));
			nks->h = (abs(t_x-1 - this->EndX) + abs(t_y - this->EndY));
			nks->f = nks->g + nks->h;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		if (isCanMove(t_x, t_y+1))
		{
			*(map + ((t_y+1) * this->X) + t_x) = 2;
			Dian* nks = new Dian();
			nks->x = t_x;
			nks->y = t_y+1;
			nks->befront = t;
			nks->g = (abs(t_x - this->StarX) + abs(t_y+1 - this->StarY));
			nks->h = (abs(t_x - this->EndX) + abs(t_y+1 - this->EndY));
			nks->f = nks->g + nks->h;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		if (isCanMove(t_x + 1, t_y))
		{
			*(map + (t_y * this->X) + t_x+1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x+1;
			nks->y = t_y;
			nks->befront = t;
			nks->g = (abs(t_x+1 - this->StarX) + abs(t_y - this->StarY));
			nks->h = (abs(t_x+1 - this->EndX) + abs(t_y - this->EndY));
			nks->f = nks->g + nks->h;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		if (isCanMove(t_x , t_y-1))
		{
			*(map + ((t_y-1) * this->X) + t_x) = 2;
			Dian* nks = new Dian();
			nks->x = t_x;
			nks->y = t_y-1;
			nks->befront = t;
			nks->g = (abs(t_x - this->StarX) + abs(t_y-1 - this->StarY));
			nks->h = (abs(t_x - this->EndX) + abs(t_y-1 - this->EndY));
			nks->f = nks->g + nks->h;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		t = NULL;
		listdian.pop_front();
		//setmap();
		if (listdian.empty())
		{
			cout << "无路可走" << endl;
		}
		//Sleep(10);
	} while (true);
}

Dian* AXXL::minF()
{
	list<Dian*>::iterator it1;
	list<Dian*>::iterator it2;
	for (it1 = listdian.begin(); it1 != listdian.end(); it1++)
	{
		for (it2 = next(it1,1); it2 != listdian.end(); it2++)
		{
			if ((*it1)->f>(*it2)->f)
			{
				swap(*it1, *it2);
			}
		}
	}
	return listdian.front();
}



void AXXL::shuchu()
{
	//system("cls");
	Dian* luxian=NULL;
	if (!listdian.empty())
	{
		luxian = listdian.front();
	}
	int i = 0;
	while (luxian!=NULL)
	{
		*(map + (luxian->y) * this->X + luxian->x) = 5;
		luxian = luxian->befront;
		i++;
	}
	while (!shifang.empty())
	{
		free(shifang.front());
		shifang.pop_front();
	}
	listdian.clear();
	shifang.clear();
	for (size_t i = 0; i < Y; i++)
	{
		for (size_t j = 0; j < X; j++)
		{
			//cout << (*(map + (i * X) + j));
			if (*(map + (i * X) + j)==5)
			{
				cout << "*";
			}
			else if (*(map + (i * X) + j) == 1)
			{
				cout << "#";
			}
			else
			{
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << endl;
	cout << i << endl;
}


#include <iostream>
#include "AXXL.h"
using namespace std;

int map[20][20] = {
					{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},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{3,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,4},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,1,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,0,0,0,0,0,0,0,0,0}
																};

int main()
{
	AXXL a(0,9,19,9,20,20,&map[0][0]);
	//a.setmap();
	a.xunlu();
	a.shuchu();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值