迷宮

思路:廣度優先搜索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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章