广度寻路八方向四正四斜方向


#include <iostream>
#include "GDXL.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,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},
					{3,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,4},
					{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,0}
};

int main()
{
	GDXL a(0, 10, 19, 10,  20, 20, &map[0][0]);
	//a.setmap();
	a.xunlu();
	a.shuchu();
	return 0;
}
#pragma once
#include <iostream>
#include <list>
#include <cmath>
#include <Windows.h>
using namespace std;
typedef struct dian
{
	int y;
	int x;
	struct dian* befront;
}Dian;
class GDXL
{
private:

	list <Dian*> listdian;
	list <Dian*> shifang;
public:
	int StarX;
	int StarY;
	int EndX;
	int EndY;
	int X;
	int Y;
	int* map;
	GDXL(int sx, int sy, int ex, int ey, int x, int y, int* pp);
	void setmap();
	bool isCanMove(int l, int r);
	void xunlu();
	void shuchu();
};

#include "GDXL.h"

GDXL::GDXL(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 GDXL::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 GDXL::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 GDXL::xunlu()
{
	int t_x;
	int t_y;
	Dian* t = new Dian;
	t->x = this->StarX;
	t->y = this->StarY;
	t->befront = NULL;
	listdian.push_back(t);
	shifang.push_back(t);
	do
	{
		if (listdian.empty())
		{
			break;
		}
		t = listdian.front();
		t_x = t->x;
		t_y = t->y;
		if (t_x == EndX && t_y == EndY)
		{
			break;
		}
		//顺时针
		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;
			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;
			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;
			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;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		if (isCanMove(t_x + 1, t_y - 1))
		{
			*(map + ((t_y - 1) * this->X) + t_x + 1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x + 1;
			nks->y = t_y - 1;
			nks->befront = t;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		if (isCanMove(t_x + 1, t_y + 1))
		{
			*(map + ((t_y + 1) * this->X) + t_x + 1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x + 1;
			nks->y = t_y + 1;
			nks->befront = t;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		
		if (isCanMove(t_x - 1, t_y + 1))
		{
			*(map + ((t_y + 1) * this->X) + t_x - 1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x - 1;
			nks->y = t_y + 1;
			nks->befront = t;
			listdian.push_back(nks);
			shifang.push_back(nks);
			nks = NULL;
		}
		
		if (isCanMove(t_x - 1, t_y - 1))
		{
			*(map + ((t_y - 1) * (this->X)) + t_x - 1) = 2;
			Dian* nks = new Dian();
			nks->x = t_x - 1;
			nks->y = t_y - 1;
			nks->befront = t;
			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);
}

void GDXL::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;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值