迷宫

思路:广度优先搜索dfs,不断从附近扩散到远处的出口。

注意:不要写成了深搜,一条道走到黑。。。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1005;//假定迷宫不会超越的最大范围
char maze[MAXN][MAXN];//迷宫存储
bool vis[MAXN][MAXN];//用于判断是否已经经过该点(1走过,0未走过)
char dir[4] = { 'D','L','R','U' };//移动方向记录,按照下、左、右、上顺序决策遍历
int dx[4] = { 1,0,0,-1 };//每次移动X的偏移量,参照字典序进行移动
int dy[4] = { 0,-1,1,0 };//每次移动Y的偏移量,参照字典序进行移动
int endX, endY;//终点座标

///*结构体:座标x,y,路径path*///
struct Position {
	int x, y;
	string path;
	Position(int newX, int newY, string newPath){//构造函数,初始化当前位置信息
		this->x = newX;
		this->y = newY;
		this->path = newPath;
	}
};

queue<Position> positionQueue;//存储当前位置以及邻里位置,以实现广度优先遍历

///*广度优先遍历迷宫寻找最短可行路径*///
string bfs(int beginX, int beginY) {
	//将起始点加入队列
	positionQueue.push(Position(beginX, beginY, ""));
	//标记起点已经走过
	vis[beginX][beginY] = true;
	//当队列非空,循环进行广度扩散
	while (positionQueue.empty() == false) {
		//获取当前队头位置
		Position current = positionQueue.front();
		//队头元素出队
		positionQueue.pop();
		//若已经到达终点,返回所求路径
		if (current.x == endX - 1 && current.y == endY - 1)return current.path;
		//尝试按照下、左、右、上四个方向走向邻近位置
		for (int i = 0; i < 4; i++) {
			//当前位置加上偏移量,移动至下一个临近位置
			int nextX = current.x + dx[i];
			int nextY = current.y + dy[i];
			//若下一个临近位置没有超出迷宫范围、不是墙壁、未曾去过
			if (nextX >= 0 && nextX < endX&&nextY >= 0 && nextY < endY&&maze[nextX][nextY] == '0'&&vis[nextX][nextY] == false) {
				//将下一个可以访问的邻居点推入队列,以此实现每一层的广度扩散(遍历)
				positionQueue.push(Position(nextX, nextY, current.path + dir[i]));
				//将下一个可以访问的邻居标记为已访问(队列已经存储,等待正式出队访问)
				vis[nextX][nextY] = true;
			}
		}
	}
}
int main() {
	//输入迷宫终点
	cin >> endX >> endY;
	//初始化为0,默认该点没有走过
	memset(vis, false, sizeof(vis));
	//输入迷宫
	for (int i = 0; i < endX; i++)
		for (int j = 0; j < endY; j++)
			cin >> maze[i][j];
	//广度优先遍历迷宫寻找最短可行路径
	string path=bfs(0, 0);
	//输出最短可行路径
	cout << path << endl;
	return 0;
}
/*
样例1:
010000
000100
001001
110000
样例1路径:DRRURRDDDR

样例2:
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
样例2路径:
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURR
DDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUUL
LLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDD
LDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
*/

 

发布了106 篇原创文章 · 获赞 136 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章