2019年第十屆藍橋杯省賽A組(C/C++組)迷宮(BFS)

試題 D: 迷宮
【問題描述】
下圖給出了一個迷宮的平面圖,其中標記爲 1 的爲障礙,標記爲 0 的爲可 以通行的地方。

010000
000100
001001
110000

迷宮的入口爲左上角,出口爲右下角,在迷宮中,只能從一個位置走到這 個它的上、下、左、右四個方向之一。
對於上面的迷宮,從入口開始,可以按DRRURRDDDR 的順序通過迷宮, 一共 10 步。其中 D、U、L、R 分別表示向下、向上、向左、向右走。

對於下面這個更復雜的迷宮(30 行 50 列),請找出一種通過迷宮的方式, 其使用的步數最少,在步數最少的前提下,請找出字典序最小的一個作爲答案。 請注意在字典序中D<L<R<U。(如果你把以下文字複製到文本文件中,請務 必檢查複製的內容是否與文檔中的一致。在試題目錄下有一個文件 maze.txt, 內容與下面的文本相同)

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

答案:

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

思路:
(1)首先01地圖中找最短路,肯定選擇廣度優先級搜索(BFS)。

(2)然後需要路徑,只需要在搜索過程中記錄下每個節點由什麼操作轉換(或者記錄前一個節點的二維座標),之後再反向沿着路徑找回去就知道了走法。

(3)最後還需要字典序最小,那麼我們可以考慮方向數組的4個方向向量的順序。想到的方法是,從右下角節點 T(n,m) 開始,向左上角S(1,1) 搜索找路徑。同時維護好每個座標點是如何由上一座標點到達。

(4)由於字典序最小,那我們安排優先走字典序小的方向,每次都走最小的,那最後結果也是最小的。

(5)由於bfs是記錄上一步的座標位置,所以我們從右下角向左上角來走,每次走最大的,那從左上角到右上角便是最小的啦。

按這樣便可以輸出路徑了!!

這道題確實坑...

(最標準的答案代碼在最後)

打印移動步驟
#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
struct box
{
	int x, y;
};
class queue//可以不用定義類,我練習一下
{
public:
	queue() { T = new box[400]; front = 0; rear = 0; }
	void push(box x)
	{
		T[rear] = x;
		rear++;
		
	}
	void pop()
	{
		front++;

		
	}
	bool isempty()
	{
		if (front  == rear)
			return true;
		else false;
	}
	box getfront()
	{
		return T[front];
	}
	box *T;
	int front;
	int rear;
};
int main()
{
	queue qu;//如果不定義的話,換成queue<box> qu;
	box q;
	int path[20][20];
	char maze[20][20];
	bool visit[20][20];
	int output[400][2];//用於輸出路徑
	memset(visit, false, sizeof(visit));
	int m, n;
	int x0, y0;//記錄起點的位置
	int di[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//上下左右四個方向

	char S[4] = { 'U','D','L','R' };
	bool flag;
	cout << "輸入迷宮的行數和列數" << endl;
	
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'E')
			{
				x0 = i, y0 = j;

			}

		}
	}
	//起點進隊列
	q.x = x0; q.y = y0;
	qu.push(q);
	visit[q.x][q.y] = true;//已經訪問過了
	//開始搜索
	flag = false;
	while (qu.isempty() != true && flag ==false)
	{
		box temp = qu.getfront();
		qu.pop();
		for (int i = 0; i < 4; i++)
		{
			q.x = temp.x + di[i][0];
			q.y = temp.y + di[i][1];
			if (q.x < 0 || q.x >= m || q.y < 0 || q.y >= n)
			{
				continue;
			}
			if (maze[q.x][q.y] != '*'&&visit[q.x][q.y] ==false)//該點能走

			{
				qu.push(q);
				path[q.x][q.y] = i;
				if (maze[q.x][q.y] == 'S')//找到目標
				{
					flag = true;
					break;
				}
				visit[q.x][q.y] = true;
			}
		}
		

	}//搜索結束
	if (flag == true)
	{
		int step = 0;
		while (q.x != x0 || q.y != y0)
		{
			switch (path[q.x][q.y])
			{
			case 0:
				cout << "D";
				q.x = q.x + 1;
				break;
			case 1:
				cout << "U";
				q.x = q.x - 1;
				break;

			case 2:
				cout << "R";
				q.y = q.y + 1;
				break;

			case 3:
				cout << "L";
				q.y = q.y - 1;
				break;
			}
			/*output[step][0] = q.x;
			output[step][1] = q.y;
			int x = q.x;
			q.x = path[q.x][q.y].x;
			q.y = path[x][q.y].y;
			step++;*/
		}
		/*cout << "輸出路徑爲:" << endl;
		for (int i = step - 1; i >= 0; --i)
		{
			cout << "(" << output[i][0] << "," << output[i][1] << ")" << endl;
		}*/
	}
	else
		cout << "迷宮無解" << endl;

	system("pause");
	return 0;
}

相似的迷宮題目

就是上面這道題的基礎版本,有利於理解上面一題

Sample Input

15 16

.**.***.***.***

***.***..**.*.*

.**.***.***.***

***.***..**.*.*

**.***.***.***.

***.***.......E

.**.***.***.*.*

***..**...*...*

.*....*.*...*.*

***.*.*.**.*..*

....*...**...**

*.*.***..**.*.*

.S*.***.***.***

***.***..**.*.*

.**.***.***.**.

***..**...*...*

Sample Output

The path is:

(11,1) (10,1) (10,2) (10,3) (9,3) (8,3) (8,4) (8,5) (9,5) (10,5) (10,6) (10,7) (9,7) (8,7) (7,7) (6,7) (5,7) (5,8) (5,9) (5,10) (5,11) (5,12) (5,13) (5,14)

首先輸入兩個數代表迷宮的行數和列數,‘.’代表可以走的點,'*'代表牆,即不可以走的點。‘S’代表迷宮的入口,‘E’代表迷宮的出口。

由於廣搜中,隊列中的每一點被搜索到時都是在離起點最短的路徑中被搜索到的,所以可以再建立一個以點的座標爲數據元素的二維數組,該二維數組每個數據元素對應迷宮中的每個點,其數據元素的值爲該點在離起點最短的路徑中的前一個點的座標。這樣迷宮的路徑便不再由隊列元素保存,所以出隊並不影響路徑的輸出。

思路類似:本質都是利用bfs記錄上一步的元素,然後輸出

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct box
{
	int x, y;
};
class queue
{
public:
	queue() { T = new box[400]; front = 0; rear = 0; }
	void push(box x)
	{
		T[rear] = x;
		rear++;
		
	}
	void pop()
	{
		front++;

		
	}
	bool isempty()
	{
		if (front  == rear)
			return true;
		else false;
	}
	box getfront()
	{
		return T[front];
	}
	box *T;
	int front;
	int rear;
};
int main()
{
	queue qu;
	box q;
	box path[20][20];
	char maze[20][20];
	bool visit[20][20];
	int output[400][2];//用於輸出路徑
	memset(visit, false, sizeof(visit));
	int m, n;
	int x0, y0;//記錄起點的位置
	int di[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//上下左右四個方向
	bool flag;
	cout << "輸入迷宮的行數和列數" << endl;
	
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'S')
			{
				x0 = i, y0 = j;

			}

		}
	}
	//起點進隊列
	q.x = x0; q.y = y0;
	qu.push(q);
	visit[q.x][q.y] = true;//已經訪問過了
	//開始搜索
	flag = false;
	while (qu.isempty() != true && flag ==false)
	{
		for (int i = 0; i < 4; i++)
		{
			q.x = qu.getfront().x + di[i][0];
			q.y = qu.getfront().y + di[i][1];
			if (q.x < 0 || q.x >= m || q.y < 0 || q.y >= n)
			{
				continue;
			}
			if (maze[q.x][q.y] != '*'&&visit[q.x][q.y] ==false)//該點能走

			{
				qu.push(q);
				path[q.x][q.y] = qu.getfront();
				if (maze[q.x][q.y] == 'E')//找到目標
				{
					flag = true;
					break;
				}
				visit[q.x][q.y] = true;
			}
		}
		qu.pop();

	}//搜索結束
	if (flag == true)
	{
		int step = 0;
		while (q.x != x0 || q.y != y0)
		{
			output[step][0] = q.x;
			output[step][1] = q.y;
			int x = q.x;
			q.x = path[q.x][q.y].x;
			q.y = path[x][q.y].y;
			step++;
		}
		cout << "輸出路徑爲:" << endl;
		for (int i = step - 1; i >= 0; --i)
		{
			cout << "(" << output[i][0] << "," << output[i][1] << ")" << endl;
		}
	}
	else
		cout << "迷宮無解" << endl;

	system("pause");
	return 0;
}

補充:!!!

確實可以記錄上一步的步驟,但我們可以走到某個點,就把這個點的路徑設爲之前的路徑加上走到這一步的路徑,最後在直接輸出最後一個點的記錄的所有路徑。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
using namespace std;
 
#define N 30
#define M 50
 
char map[N][M];
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};//D<L<R<U
char ch[4]={'D','L','R','U'};
int vis[N][M]={0};
 
struct point
{
	int x,y;
	string road;//記錄路徑到這一個點的路徑
	point(int a,int b)
	{
		x=a;
		y=b;
	}
};
 
void bfs()
{
	queue<point> q;
	point p(0,0);
	p.road="";
	q.push(p);
	vis[0][0]=1;
	while(!q.empty())
	{
		point t=q.front();
		q.pop();
		if(t.x==N-1&&t.y==M-1)
		{
			cout<<t.road<<endl;//輸出
			break;
		}
		for(int i=0;i<4;i++)
		{
			int dx=t.x+dir[i][0];
			int dy=t.y+dir[i][1];
			if(dx>=0&&dx<N&&dy>=0&&dy<M)
			{
				if(map[dx][dy]=='0'&&!vis[dx][dy])
				{
					point tt(dx,dy);
					tt.road=t.road+ch[i];//記錄路徑
					q.push(tt);
					vis[dx][dy]=1;
				}
			}
		}
	}
}
 
int main()
{
	for(int i=0;i<N;i++)
	{
		cin>>map[i];
	}
	bfs();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章