队列的应用——求解迷宫问题

代码示例:

#include <iostream>
using namespace std;
const int MaxSize = 20;				//迷宫最大行、列数
const int QueueSize = 100;			//顺序队大小
struct Box							//方块结构体类型
{
	int i;							//方块的行号
	int j;							//方块的列号
	int pre;						//本路径中上一方块在队列中的下标
};
class Queue							//非循环顺序队列类
{
	Box *data;						//存放队中方块
	int front, rear;				//队头队尾指针
public:
	Queue();						//构造函数:队列初始化
	~Queue();						//析构函数:释放队列空间
	bool QueueEmpty();				//判断队列是否为空
	void edQueue(int x, int y, int pre1);	//进队一个方块
	void deQueue(int &x, int &y, int &cfront);	//出队一个方块
	void DispBox(int cfront);		//从cfront出发找一条迷宫路径
	void DispQueue();				//用于输出队中所有元素,调试用
};
Queue::Queue()										//构造函数:队列初始化
{
	data = new Box[QueueSize];
	front = rear = -1;
}
Queue::~Queue()										//析构函数:释放队列空间
{
	delete[] data;
}
bool Queue::QueueEmpty()							//判断队列是否为空
{
	return(front == rear);
}
void Queue::edQueue(int x, int y, int pre1)			//进队一个方块
{
	rear++;
	data[rear].i = x;
	data[rear].j = y; 
	data[rear].pre = pre1;
}
void Queue::deQueue(int &x, int &y, int &cfront)	//出队一个方块
{
	front++;
	x = data[front].i; 
	y = data[front].j;
	cfront = front;
}
void Queue::DispBox(int cfront)						//从cfront出发找一条迷宫路径
{
	Box path[QueueSize];
	int k, d = -1;
	k = cfront;
	while (k != -1)									//找到入口为止
	{
		d++;										//将一个方块保存到path中
		path[d].i = data[k].i; 
		path[d].j = data[k].j;
		k = data[k].pre;							//回退找上一个方块
	}
	cout << "一条迷宫路径如下:\n";
	for (k = d; k >= 0; k--)						//反向输出构成一条正向的迷宫路径
	{
		cout << "  (" << path[k].i << "," << path[k].j << ")";
		if ((d - k + 1) % 5 == 0) cout << endl;		//每行输出5个方块
	}
	cout << endl;
}
void Queue::DispQueue()								//用于输出队中所有元素,调试用
{
	int i;
	cout << "队中所有元素:\n";
	for (i = 0; i <= rear; i++)
		cout << "  " << i << " (" << data[i].i << "," << data[i].j << ")  " << data[i].pre << endl;
}
class Maze2											//用队列求解一条迷宫路径类
{
	int a[MaxSize][MaxSize];						//迷宫数组
	int m, n;										//迷宫行列数
public:
	void Seta(int mg[][MaxSize], int m1, int n1);	//设置迷宫数组
	bool mgpath(int xi, int yi, int xe, int ye);
};
void Maze2::Seta(int mg[][MaxSize], int m1, int n1)	//设置迷宫数组
{
	int i, j;
	m = m1;
	n = n1;
	for (i = 0; i<m; i++)
		for (j = 0; j<n; j++)
			a[i][j] = mg[i][j];
}
bool Maze2::mgpath(int xi, int yi, int xe, int ye)
{
	int i, j, di, cfront, i1, j1;
	Queue qu;										//创建一个空队qu
	qu.edQueue(xi, yi, -1);							//入口进队,其pre置为-1
	a[xi][yi] = -1;									//为避免来回找相邻方块,将进队的方块置为-1
	while (!qu.QueueEmpty())						//队不空时循环
	{
		qu.deQueue(i, j, cfront);					//出队一个方块,该方块仍在队列中
		if (i == xe && j == ye)						//找到了出口,输出路径
		{
			qu.DispBox(cfront);
			qu.DispQueue();
			return true;							//找到一条路径时返回true
		}
		for (di = 0; di<4; di++)					//循环扫描每个方位,把每个可走的方块进队
		{
			switch (di)
			{
				case 0:i1 = i - 1; j1 = j;   break;
				case 1:i1 = i;   j1 = j + 1; break;
				case 2:i1 = i + 1; j1 = j;   break;
				case 3:i1 = i;   j1 = j - 1; break;
			}
			if (a[i1][j1] == 0)						//找到一个相邻可走方块
			{
				qu.edQueue(i1, j1, cfront);			//将该相邻方块进队,并置其pre为父方块下标cfront
				a[i1][j1] = -1;						//为避免来回找相邻方块,将进队的方块置为-1
			}
		}
	}
	return false;									//未找到任何路径时返回false
}
void main()
{
	int mg[][MaxSize] = 
	{ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
	{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 }, { 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
	{ 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 1, 0, 0, 1 }, { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
	{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
	Maze2 mz;									//创建一个Maze2对象mz
	mz.Seta(mg, 10, 10);						//设置迷宫数组
	cout << "求(1,1)到(8,8)的迷宫路径\n";
	if (!mz.mgpath(1, 1, 8, 8))					//求入口为(1,1)出口为(8,8)的迷宫路径
		cout << "不存在迷宫路径\n";
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值