第十屆藍橋杯省賽C/C++ 大學B組 E題 迷宮
【問題描述】 下圖給出了一個迷宮的平面圖,其中标記為 1 的為障礙,标記為 0 的為可 以通行的地方。
010000
000100
001001
110000
迷宮的入口為左上角,出口為右下角,在迷宮中,隻能從一個位置走到這 個它的上、下、左、右四個方向之一。 對于上面的迷宮,從入口開始,可以按DRRURRDDDR 的順序通過迷宮, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 對于下面這個更複雜的迷宮(30 行 50 列),請找出一種通過迷宮的方式, 其使用的步數最少,在步數最少的前提下,請找出字典序最小的一個作為答案。 請注意在字典序中D<L<R<U。
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
這題是第十屆藍橋杯省賽的E題,一個迷宮問題,要求找出字典序的最短路徑,解法比較直接,但是bfs寫的少的話,可能會有一點難度,主要使用的就是帶路徑的bfs。搜尋完BFS之後,進行遞歸的往回求解路徑。
得到的答案為:
DDDDRRURRRRRRRDRRRDDDLDDRDDDDDDDDDDDDRDRDRRUUURRRRDDDDRDRRRRRURRRDRRDDDRRRRUURUUUUUUUULLLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDRDRRRRDRDRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
char data[30][50];
bool visit[30][50]={false};
char ans[30][50];
map<char ,int >dir_pre;
typedef struct store_ds{
int row;//行
int col;//列
char dir;//到達這個位置走的方向
}store_ds;
int next_direction[4][2]={//字典序下一次走的方向
{1,0},//D
{0,-1},//L
{0,1},//R
{-1,0}//U
};
int pre_direction[4][2]={//上一次走向,回溯路徑
{-1,0},//D
{0,1},//L
{0,-1},//R
{1,0}//U
};
queue<store_ds> que;
char direction[4]={'D','L','R','U'};//四個方向,字典序
bool is_ok(int row,int col){
return (row >= 0 && row <= 29)&&(col >= 0 && col <= 49) && data[row][col] == '0' && !visit[row][col]? true : false;
}
void bfs(){
// D,L,R,U
store_ds n = que.front();//取出隊頭
while(!(n.row==29&&n.col==49)){
n = que.front();//取出隊頭
if(n.row==29&&n.col==49){
break;
}
que.pop();
visit[n.row][n.col] = true;//通路隊頭
ans[n.row][n.col]=n.dir;
for(int i = 0;i < 4; i++){
if(is_ok(n.row+next_direction[i][0],n.col+next_direction[i][1])){//如果可以走,則加入隊列
store_ds mid;
mid.row = n.row+next_direction[i][0];
mid.col = n.col+next_direction[i][1];
mid.dir = direction[i];
que.push(mid);
}
}
}
}
int main(){
ifstream input("/Users/qiguan/A.txt");//讀取檔案
for(int i = 0; i < 30; i++){
for(int j = 0; j < 50; j++){
input>>data[i][j];
}
}
store_ds mid_1;
mid_1.row = 0;
mid_1.col = 0;
que.push(mid_1);
bfs();
//搜尋出結果後,往前回溯出路徑
dir_pre['D']=0;
dir_pre['L']=1;
dir_pre['R']=2;
dir_pre['U']=3;
string re="";
int row =29,col = 49;
re += que.front().dir;
row += pre_direction[dir_pre[que.front().dir]][0];
col += pre_direction[dir_pre[que.front().dir]][1];
while(!(row==0&&col==0)){
char dir = ans[row][col];
re +=dir;
row += pre_direction[dir_pre[dir]][0];
col += pre_direction[dir_pre[dir]][1];
}
for(int i = re.length()-1;i>=0 ;i--)
cout<<re[i];
return 0;
}