栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

栈和队列实现迷宫路径搜索

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栈和队列实现迷宫路径搜索

栈和队列实现迷宫路径搜索
  • 题目
  • 栈实现
    • 思想
    • 代码实现
  • 队列实现
    • 思想
    • 代码实现
  • 完整代码

题目

自定义一个迷宫,行、列值>8,它有一个出口和一个入口,先输出迷宫图形,然后找出一条从入口到出口的路径
基本接口:

class MazeWay {
public:
	// 定义迷宫
	vector> mazeSet = { {1,1,1,1,1,1,1,1,1,1,1},{1,1,0,0,0,0,0,0,0,0,1},
									{1,1,0,0,1,1,1,1,1,0,1},{1,1,1,0,0,0,1,0,1,0,1},
									{1,1,1,1,0,1,0,1,1,1,1},{1,0,1,0,0,0,0,0,0,0,1},
									{1,0,1,0,1,1,1,1,1,0,1},{1,0,0,0,1,0,0,0,1,0,1},
									{1,1,1,1,1,0,1,0,1,0,1},{1,0,0,0,1,0,1,0,1,0,1},
									{1,0,1,0,0,0,1,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1}};
	//vector> mazeSet = { {1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,1},
	//								  {1,0,0,0,0,0,1,1,1,1,1,0,1},{1,1,1,1,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,1,0,1,0,0,0,0,0,1},{1,0,0,0,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,1,0,1,0,0,0,0,0,1},{1,0,1,1,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,1,0,1,1,1,1,1,0,1},
	//								  {1,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1} };
	//pair beginN{ 5,3 },endN{3,7};
	pair beginN{ 3,9 }, endN{ 10,1 };										// 定义终点和起始点
	void showMaze(vector> mazeGet);										// 迷宫可视化
	int findWay(vector> mazeMap, pair &place);				// 查找路线
	stack> mazePathStack(vector> mazeGet);				// 主函数(栈实现)
	vector> mazePathQueue(vector> mazeGet);				// 主函数(队列实现)
};
栈实现 思想

通过回溯法进行路径搜索,先朝一个指定的方向进行不断的前行,如果没有找到路,不断沿走过的路回退,找到没有走过的路,寻找新的路去走。
额,可能表述的不是很清楚,具体可以Baidu一下回溯法,有很多大神讲的不错。

代码实现
int MazeWay::findWay(vector> mazeMap, pair& place)
{
	// 上
	if (mazeMap[place.first - 1][place.second] == 0) {
		place.first--;
		return 1;
	}
	// 右
	else if (mazeMap[place.first][place.second + 1] == 0) {
		place.second++;
		return 1;
	}
	// 下
	else if (mazeMap[place.first + 1][place.second] == 0) {
		place.first++;
		return 1;
	}
	// 左
	else if (mazeMap[place.first][place.second - 1] == 0) {
		place.second--;
		return 1;
	}
	return 0;
}
// 栈实现迷宫路径查找
stack> MazeWay::mazePathStack(vector> mazeGet)
{
	stack> waySign;
	waySign.push(beginN);
	pair NodeSin;
	while (!waySign.empty()) {
		NodeSin = waySign.top();
		if (NodeSin == endN)return waySign;			// 找到终点,出口
		mazeGet[NodeSin.first][NodeSin.second]--;	// 走过的路做标记
		findWay(mazeGet, NodeSin);					// 找没有走过的路
		if (NodeSin != waySign.top()) {				// 如果找到了路
			waySign.push(NodeSin);					// 插入新路
		}
		else waySign.pop();							// 没有找到路,退回重新找路
	}
	return stack>();					// 没有找到路,返回空
}
队列实现 思想

通过广度优先的方式进行路径搜索,将所有可以走的路进行搜索,将现在所有可能走的路存储到队列,然后再不断提取和存储新的可能路线。
如果还是觉得不太理解的话,也还是百度吧(泪奔)

代码实现
vector> MazeWay::mazePathQueue(vector> mazeGet)
{
	queue>> waySearch;
	vector> waySave = { beginN };
	waySearch.push(waySave);
	while (!waySearch.empty())
	{
		waySave = waySearch.front();
		waySearch.pop();
		for (int i = -1; i < 2; i ++) {
			for (int j = -1; j < 2; j++)
			{
				if (i && j)continue;
				auto waySet = waySave;
				pair node = { waySet.back().first + i ,waySet.back().second + j };
				if (node.first == endN.first && node.second == endN.second) {
					waySet.push_back(node);
					return waySet;
				}
				if (!mazeGet[node.first][node.second]) {
					mazeGet[node.first][node.second] = 1;
					waySet.push_back(node);
					waySearch.push(waySet);
				}
			}
		}
	}
	return vector>();
}
完整代码
#include
#include
#include
#include
#include
using namespace std;




class MazeWay {
public:
	// 定义迷宫
	vector> mazeSet = { {1,1,1,1,1,1,1,1,1,1,1},{1,1,0,0,0,0,0,0,0,0,1},
									{1,1,0,0,1,1,1,1,1,0,1},{1,1,1,0,0,0,1,0,1,0,1},
									{1,1,1,1,0,1,0,1,1,1,1},{1,0,1,0,0,0,0,0,0,0,1},
									{1,0,1,0,1,1,1,1,1,0,1},{1,0,0,0,1,0,0,0,1,0,1},
									{1,1,1,1,1,0,1,0,1,0,1},{1,0,0,0,1,0,1,0,1,0,1},
									{1,0,1,0,0,0,1,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1}};
	//vector> mazeSet = { {1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,1},
	//								  {1,0,0,0,0,0,1,1,1,1,1,0,1},{1,1,1,1,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,1,0,1,0,0,0,0,0,1},{1,0,0,0,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,1,0,1,0,0,0,0,0,1},{1,0,1,1,1,0,1,0,0,0,0,0,1},
	//								  {1,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,1,0,1,1,1,1,1,0,1},
	//								  {1,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1} };
	//pair beginN{ 5,3 },endN{3,7};
	pair beginN{ 3,9 }, endN{ 10,1 };										// 定义终点和起始点
	void showMaze(vector> mazeGet);										// 迷宫可视化
	int findWay(vector> mazeMap, pair &place);				// 查找路线
	stack> mazePathStack(vector> mazeGet);				// 主函数(栈实现)
	vector> mazePathQueue(vector> mazeGet);				// 主函数(队列实现)
};

int MazeWay::findWay(vector> mazeMap, pair& place)
{
	// 上
	if (mazeMap[place.first - 1][place.second] == 0) {
		place.first--;
		return 1;
	}
	// 右
	else if (mazeMap[place.first][place.second + 1] == 0) {
		place.second++;
		return 1;
	}
	// 下
	else if (mazeMap[place.first + 1][place.second] == 0) {
		place.first++;
		return 1;
	}
	// 左
	else if (mazeMap[place.first][place.second - 1] == 0) {
		place.second--;
		return 1;
	}
	return 0;
}
// 栈实现迷宫路径查找
stack> MazeWay::mazePathStack(vector> mazeGet)
{
	stack> waySign;
	waySign.push(beginN);
	pair NodeSin;
	while (!waySign.empty()) {
		NodeSin = waySign.top();
		if (NodeSin == endN)return waySign;			// 找到终点,出口
		mazeGet[NodeSin.first][NodeSin.second]--;	// 走过的路做标记
		findWay(mazeGet, NodeSin);					// 找没有走过的路
		if (NodeSin != waySign.top()) {				// 如果找到了路
			waySign.push(NodeSin);					// 插入新路
		}
		else waySign.pop();							// 没有找到路,退回重新找路
	}
	return stack>();// 没有找到路,返回空
}

vector> MazeWay::mazePathQueue(vector> mazeGet)
{
	queue>> waySearch;
	vector> waySave = { beginN };
	waySearch.push(waySave);
	while (!waySearch.empty())
	{
		waySave = waySearch.front();
		waySearch.pop();
		for (int i = -1; i < 2; i ++) {
			for (int j = -1; j < 2; j++)
			{
				if (i && j)continue;
				auto waySet = waySave;
				pair node = { waySet.back().first + i ,waySet.back().second + j };
				if (node.first == endN.first && node.second == endN.second) {
					waySet.push_back(node);
					return waySet;
				}
				if (!mazeGet[node.first][node.second]) {
					mazeGet[node.first][node.second] = 1;
					waySet.push_back(node);
					waySearch.push(waySet);
				}
			}
		}
	}
	return vector>();
}



void MazeWay::showMaze(vector> mazeGet)	// 迷宫可视化
{
	for (int i = 0; i < mazeGet.size(); i++) {

		for (int j = 0; j < mazeGet[0].size(); j++) {
			if (i == beginN.first && j == beginN.second)
			{
				cout << "起";
				continue;
			}
			else if (i == endN.first && j == endN.second)
			{
				cout << "终";
				continue;
			}
			else if (mazeGet[i][j]==1)cout << "■";
			else if (mazeGet[i][j] == 0)cout << "  ";
			else cout << "╬ ";
		}
		cout << "n";
	}
}


int main() {
	MazeWay run_queue;
	auto wayGetQueue = run_queue.mazePathQueue(run_queue.mazeSet);
	cout << "原始迷宫:" << endl;
	run_queue.showMaze(run_queue.mazeSet);
	if (wayGetQueue.empty())cout << "没有找到路" << endl;
	else {
		for(auto node: wayGetQueue){
			run_queue.mazeSet[node.first][node.second] = -1;
		}
		cout << "nnn队列结果:" << endl;
		run_queue.showMaze(run_queue.mazeSet);
	}
	MazeWay run_way;
	auto wayGetStack = run_way.mazePathStack(run_way.mazeSet);
	if (wayGetStack.empty())cout << "没有找到路" << endl;
	else {
		while (!wayGetStack.empty())
		{
			auto node = wayGetStack.top();
			wayGetStack.pop();
			run_way.mazeSet[node.first][node.second] = -1;
		}
		cout << "nnn栈结果:" << endl;
		run_way.showMaze(run_way.mazeSet);
	}
	
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873519.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号