试题 E: 迷宫
【問題描述】
下圖給出了一個(gè)迷宮的平面圖,其中標(biāo)記為 1 的為障礙,標(biāo)記為 0 的為可
以通行的地方。
010000
000100
001001
110000
迷宮的入口為左上角,出口為右下角,在迷宮中,只能從一個(gè)位置走到這
個(gè)它的上、下、左、右四個(gè)方向之一。
對(duì)于上面的迷宮,從入口開始,可以按DRRURRDDDR 的順序通過迷宮,
一共 10 步。其中 D、U、L、R 分別表示向下、向上、向左、向右走。
對(duì)于下面這個(gè)更復(fù)雜的迷宮(30 行 50 列),請(qǐng)找出一種通過迷宮的方式,
其使用的步數(shù)最少,在步數(shù)最少的前提下,請(qǐng)找出字典序最小的一個(gè)作為答案。
請(qǐng)注意在字典序中D<L<R<U。(如果你把以下文字復(fù)制到文本文件中,請(qǐng)務(wù)
必檢查復(fù)制的內(nèi)容是否與文檔中的一致。在試題目錄下有一個(gè)文件 maze.txt,
內(nèi)容與下面的文本相同)
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
試題E: 迷宮 7
第十屆藍(lán)橋杯大賽軟件類省賽 C/C++ 大學(xué) B 組
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
【答案提交】
這是一道結(jié)果填空的題,你只需要算出結(jié)果后提交即可。本題的結(jié)果為一
個(gè)字符串,包含四種字母 D、U、L、R,在提交答案時(shí)只填寫這個(gè)字符串,填
寫多余的內(nèi)容將無法得分。
這題沒啥難的,然而當(dāng)時(shí)還是寫錯(cuò)了。拿著鍵盤就是干,瞎搜就行了,注意下字典序順序就好
1 #include<bits/stdc++.h> 2 const int maxn=1000; 3 using namespace std; 4 string mp[maxn]; 5 int loc[][2]= {1,0,0,-1,0,1,-1,0}; 6 char dir[10]= {'D', 'L', 'R', 'U'}; 7 int n,m; 8 int dist[maxn][maxn]; 9 void bfs() 10 { 11 memset(dist,-1,sizeof(dist)); 12 dist[n-1][m-1]=0; 13 queue< pair<int,int> >que; 14 que.push({n-1,m-1}); 15 while(!que.empty()) 16 { 17 pair<int,int>a,now; 18 now=que.front(); 19 que.pop(); 20 for(int i = 0; i < 4; i ++ ) 21 { 22 int x=now.first+loc[i][0]; 23 int y=now.second+loc[i][1]; 24 if(x>=0&&x<n&&y<m&&y>=0&&mp[x][y]=='0'&&dist[x][y]==-1) 25 { 26 dist[x][y]=dist[now.first][now.second]+1; 27 que.push({x,y}); 28 } 29 } 30 } 31 return ; 32 } 33 34 int main () 35 { 36 string ans; 37 scanf("%d%d",&n,&m); 38 for(int i = 0 ; i < n ; i++ ) 39 cin >> mp[i]; 40 bfs(); 41 int x=0,y=0; 42 while(x!=n-1||y!=m-1) 43 { 44 for(int i=0 ;i < 4; i++ ) 45 { 46 int x1=x+loc[i][0],y1=y+loc[i][1]; 47 if(dist[x][y]==dist[x1][y1]+1&&mp[x1][y1]=='0') 48 { 49 x=x1,y=y1; 50 ans+=dir[i]; 51 break; 52 } 53 54 } 55 } 56 cout<<ans<<endl; 57 58 return 0; 59 } 60 //DDDDRRURRRRRRDRRRRDD 61 //DLDDRDDDDDDDDDDDDRDD 62 //RRRURRUURRDDDDRDRRRR 63 //RRDRRURRDDDRRRRUURUU 64 //UUUUULULLUUUURRRRUUL 65 //LLUUUULLUUULUURRURRU 66 //RURRRDDRRRRRDDRRDDLL 67 //LDDRRDDRDDLDDDLLDDLL 68 //LDLDDDLDDRRRRRRRRRDD 69 //DDDDRR?
轉(zhuǎn)載于:https://www.cnblogs.com/yuanlinghao/p/10595333.html
總結(jié)
- 上一篇: 亚马逊跟卖软件系统
- 下一篇: 数据结构与算法分析之---部分排序算法的