迷宫找出口(BFS最短路)

#include <iostream>
#include <vector>
#define COLS 10
#define ROWS 10
using namespace std;

struct MyPoint
{
	int y;
	int x;
	friend bool operator==(const MyPoint& p1, const MyPoint& p2);
};
bool operator==(const MyPoint& p1, const MyPoint& p2)
{
	if (p1.x == p2.x && p1.y == p2.y)return true;
	return false;
}
enum dirent{p_up,p_left,p_down,p_right};
struct pathNode
{
	//int dir;//当前试探方向
	bool isFind; //是否走过 
};

//四叉树节点类型
struct treeNode
{
	MyPoint   pos;
	treeNode * pParent;//父指针
	vector<treeNode*> child;//指针数组,保存指向当前节点的孩子节点的指针
	//treeNode(){}
	treeNode(int x, int y)
	{
		pos.x = x; pos.y = y;
		pParent = nullptr;
	}
	treeNode(MyPoint p)
	{
		pos = p;
		pParent = nullptr;
	}
	
};

//判断某个点能不能走
bool canWalk(int map[ROWS][COLS], pathNode pathMap[ROWS][COLS], MyPoint pos);
#if 0
treeNode* createTreeNode(int x, int y)
{
	treeNode* pNew = new treeNode;
	memset(pNew, 0, sizeof(treeNode));
	pNew->pos.x = x;
	pNew->pos.y = y;
}
#endif
int main()
{

	int map[ROWS][COLS] = {
	{0,1,1,1,1,1,1,1,1,1},
	{0,0,0,0,1,1,0,0,0,1},
	{0,0,1,0,1,1,0,1,0,1},
	{0,0,1,0,1,1,0,1,0,1},
	{0,0,0,0,1,1,0,0,0,1},
	{0,0,1,0,1,1,0,1,1,1},
	{0,0,1,0,1,1,0,1,1,1},
	{0,0,1,0,1,1,0,1,1,1},
	{0,0,0,0,0,0,0,0,0,1},
	{0,0,0,0,0,0,0,0,0,1},
	};
	//1.2辅助地图(记录当前试探方向,记录有没有走过)
	pathNode pathMap[ROWS][COLS] = { 0 };
		//1.4七点 终点
		MyPoint begPos = { 1,1 };
		MyPoint endPos = { 1,8 };
	//1、创建一颗四叉树:存储寻路过程中所有的节点,找到终点后,找出路径

		treeNode* pRoot = nullptr; //可以通过它展开整棵树

								   //标记起点走过
		pathMap[begPos.y][begPos.x].isFind = true;
		//起点成为树的根节点
		pRoot = new treeNode(begPos);
	//2、准备一个数组:存储当前层的节点

		//当前层
		vector<treeNode*>current;
		current.push_back(pRoot);//把树的根节点存放到当前层中。
		//下一层
		vector<treeNode*>next;

	//3、寻路
		MyPoint currentPos;//当前点
		MyPoint tempPos;

		treeNode* pChild = nullptr;//专门用来存储新节点

		bool isFindEnd = false;
		while (1)
		{
			next.clear();//清空这个数组。
			//3.2当前层的每一个
			for (int i=0;i<current.size();i++)
			{
				currentPos = current[i]->pos;
				for (int j = 0; j < 4; j++)
				{
				//3.2.1从当前点引发出四个点
					switch (j)
					{
					case p_up:
							tempPos.x = currentPos.x;
							tempPos.y = currentPos.y-1;
							break;
					case p_down:
						tempPos.x = currentPos.x;
						tempPos.y = currentPos.y + 1;
						break;
					case p_left:
						tempPos.x = currentPos.x-1;
						tempPos.y = currentPos.y ;
						break;
					case p_right:
						tempPos.x = currentPos.x+1;
						tempPos.y = currentPos.y ;
						break;
					}
					if (canWalk(map, pathMap, tempPos))//3.2判断tempPos能不能走
						
					{
						//能走
						//3.2.2.1创建新节点
						pChild=new treeNode(tempPos);
						//3.2.2.2新节点入树
						//3.2.2.2.1当前节点的孩子指向
						current[i]->child.push_back(pChild);
						//3.2.2.2.2新节点的父指针指向当前节点
						pChild->pParent = current[i];

						//3.2.2.3标记走过
						pathMap[tempPos.y][tempPos.x].isFind = true;
						
						//3.2.2.4存入下一层的数组中
						next.push_back(pChild);
						
						//3.3如果有一个点是终点,可以结束了。
						if (tempPos == endPos)
						{
							isFindEnd = true;
							break;
						}
					}

				}//end of for(j)
				if (isFindEnd)break;

				
			}//end of for(i)
			//3.4如果新的一层节点数为0,地图已经被探索完毕,整个寻路过程结束
			if (!next.size())break;
			if (isFindEnd)break;
			current = next;//切换到下一层

		}//end of while(1)
		 
		 //4显示路径
		if (isFindEnd)
		{
			printf("找到终点啦!\n");
			printf("Path:");

			while (pChild)
			{
				printf("(%d,%d)", pChild->pos.y, pChild->pos.x);
				pChild = pChild->pParent;
			}
			printf("\n");
		}
		
	return 0;
}
//判断某个点能不能走
bool canWalk(int map[ROWS][COLS], pathNode pathMap[ROWS][COLS], MyPoint pos)
{
	//不在地图范围内
	if (pos.y < 0 || pos.y >= ROWS || pos.x < 0 || pos.x >= COLS)return false;
	//走过
	if (pathMap[pos.y][pos.x].isFind)return false;
	//是障碍
	if (map[pos.y][pos.x])return false;
	return true;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值