福州大学第十五届程序设计竞赛_重现赛B题迷宫寻宝
Problem B 迷宫寻宝
Accept: 52 Submit: 183
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入口、一些墙壁以及一个宝藏。由于迷宫是四连通的,即在迷宫中的一个位置,只能走到与它直接相邻的其他四个位置(上、下、左、右)。现洪尼玛在迷宫的入口处,问他最少需要走几步才能拿到宝藏?若永远无法拿到宝藏,则输出-1。
Input
多组测试数据。
输入第一行为正整数n,表示迷宫大小。
接下来n行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'#'表示该位置为墙壁,字符'S'表示该位置为入口,字符'E'表示该位置为宝藏,输入数据中只有这四种字符,并且'S'和'E'仅出现一次。
n≤1000
Output
输出拿到宝藏最少需要走的步数,若永远无法拿到宝藏,则输出-1。
Sample Input
5
S.#..
#.#.#
#.#.#
#...E
#....
Sample Output
7
分析:DFS,BFS基础题型,下面加一个poj2251,加深对简单搜索的理解,三维空间是3维数组,如果加上钥匙或者传送门的话就是四维空间
详情见: 2018年长沙理工大学G题 (找钥匙) 2018年湘潭大学F题 (传送门)
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <math.h> 7 #include <queue> 8 using namespace std; 9 10 #define FF(i, a, b) for(int i = a; i < b; i++) 11 #define RR(i, a, b) for(int i = a; i > b; i++) 12 #define ME(a, b) memset(a, b, sizeof(a)) 13 #define SC(x) scanf("%d", &x) 14 #define PR(x) printf("%d\n", x) 15 #define INF 0x3f3f3f3f 16 #define MAX 1100 17 #define MOD 1000000007 18 #define E 2.71828182845 19 #define M 8 20 #define N 6 21 typedef long long LL; 22 const double PI = acos(-1.0); 23 typedef pair<int, int> Author; 24 vector<pair<string, int> > VP; 25 26 struct Node{ 27 int x, y, step; 28 }; 29 Node bg, ed, p1, p2; 30 queue<Node> q; 31 32 char matrix[MAX][MAX]; 33 int dir[4][2] = {1,0,-1,0,0,1,0,-1}; 34 int dis[MAX][MAX]; 35 int n, m, ans; 36 37 int check(int x, int y){ 38 if(x < 0 || x >= n || y < 0 || y >= m || dis[x][y] || matrix[x][y] == '#') return 0; 39 return 1; 40 } 41 void Clear(queue<Node>& q){ 42 queue<Node> empty; 43 swap(empty, q); 44 } 45 46 int BFS(){ 47 p1 = bg; 48 q.push(p1);dis[p1.x][p1.y] = 1; 49 while(!q.empty()){ 50 p2 = q.front();q.pop(); 51 if(p2.x == ed.x && p2.y == ed.y) return p2.step; //Í˳ö 52 for(int i = 0; i < 4; i++){ 53 Node p3; 54 p3.x = p2.x + dir[i][0]; 55 p3.y = p2.y + dir[i][1]; 56 p3.step = p2.step; 57 if(!check(p3.x, p3.y)) continue; 58 dis[p3.x][p3.y] = 1; 59 p3.step = p2.step + 1; 60 q.push(p3); 61 } 62 } 63 return -1; 64 } 65 66 int main(void){ 67 // #ifdef LOCAL 68 // freopen("in.txt", "r", stdin); 69 // freopen("out.txt", "w", stdout); 70 // #endif 71 ios::sync_with_stdio(false); cin.tie(0); 72 int i, j, k; 73 74 while(cin>>n){ 75 m = n; 76 Clear(q);ME(dis, 0); 77 78 for(i = 0; i < n; i++){ 79 for(j = 0; j < m; j++){ 80 cin>>matrix[i][j]; 81 if(matrix[i][j] == 'S'){bg.x = i; bg.y = j; bg.step = 0;} 82 else if(matrix[i][j] == 'E'){ed.x = i; ed.y = j;} 83 } 84 } 85 86 ans = BFS(); 87 cout<<ans<<endl; 88 } 89 return EXIT_SUCCESS; 90 }
POJ2251:三维空间找'S'到‘E’的距离,差不多的解法。
1 /** 2 3 :;LaEaHKEEGpPXU7;, 4 .:75pKH11252U252XapZgRQgD6XJscLr;,. 5 :LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7. 6 .r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r . 7 ,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXUL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,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. ..... 20 JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... . 21 .Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr ....... 22 1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:....... 23 D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ... 24 :Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. ....... 25 sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. .......... 26 OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;.............. 27 .BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,............. 28 1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... ............. 29 ..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... .. 30 ,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........ 31 .:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,. 32 .;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,. 33 ,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. .. 34 rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2 35 ;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr 36 r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77; 37 .:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc 38 .:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c 39 .:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw 40 ;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2 41 J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU 42 JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J 43 U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1 44 2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ 45 a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U 46 5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5 47 U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2 48 s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS 49 s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO 50 a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP 51 1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6 52 :K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ 53 w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD 54 H: ;XJ;77rv7rJUaQ;rgaPgXXHBB; . . .ri:;; ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG 55 iwsJs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68 .:J: :; .,:K6BQS7:.,.,. 69 :r7Ji;r7;::;;. 70 . **/ 71 72 #include <iostream> 73 #include <algorithm> 74 #include <vector> 75 #include <stdlib.h> 76 #include <string.h> 77 #include <math.h> 78 #include <queue> 79 using namespace std; 80 81 #define FF(i, a, b) for(int i = a; i < b; i++) 82 #define RR(i, a, b) for(int i = a; i > b; i++) 83 #define ME(a, b) memset(a, b, sizeof(a)) 84 #define SC(x) scanf("%d", &x) 85 #define PR(x) printf("%d\n", x) 86 #define INF 0x3f3f3f3f 87 #define MAX 35 88 #define MOD 1000000007 89 #define E 2.71828182845 90 #define M 8 91 #define N 6 92 typedef long long LL; 93 const double PI = acos(-1.0); 94 typedef pair<int, int> Author; 95 vector<pair<string, int> > VP; 96 97 struct Node{ 98 int x, y, z, step; 99 }; 100 Node bg, ed, p1, p2; 101 queue<Node> q; 102 103 char matrix[MAX][MAX][MAX]; 104 int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; 105 int dis[MAX][MAX][MAX]; 106 int n, m, l, ans; 107 108 int check(int x, int y, int z){ 109 if(x < 0 || x >= n || y < 0 || y >= m || z < 0 || z >= l || dis[x][y][z] || matrix[x][y][z] == '#') return 0; 110 return 1; 111 } 112 void Clear(queue<Node>& q){ 113 queue<Node> empty; 114 swap(empty, q); 115 } 116 117 int BFS(){ 118 p1 = bg; 119 q.push(p1);dis[p1.x][p1.y][p1.z] = 1; 120 while(!q.empty()){ 121 p2 = q.front();q.pop(); 122 if(p2.x == ed.x && p2.y == ed.y && p2.z == ed.z) return p2.step; //退出 123 for(int i = 0; i < 6; i++){ 124 Node p3; 125 p3.x = p2.x + dir[i][0]; 126 p3.y = p2.y + dir[i][1]; 127 p3.z = p2.z + dir[i][2]; 128 p3.step = p2.step; 129 if(!check(p3.x, p3.y, p3.z)) continue; 130 dis[p3.x][p3.y][p3.z] = 1; 131 p3.step = p2.step + 1; 132 q.push(p3); 133 } 134 } 135 return 0; 136 } 137 138 int main(void){ 139 #ifdef LOCAL 140 freopen("in.txt", "r", stdin); 141 freopen("out.txt", "w", stdout); 142 #endif 143 ios::sync_with_stdio(false); cin.tie(0); 144 int i, j, k; 145 146 while(cin>>l>>n>>m){ 147 if(l <= 0 || n <= 0 || m <= 0) break; 148 Clear(q);ME(dis, 0); 149 150 for(k = 0; k < l; k++){ 151 for(i = 0; i < n; i++){ 152 for(j = 0; j < m; j++){ 153 cin>>matrix[i][j][k]; 154 if(matrix[i][j][k] == 'S'){bg.x = i; bg.y = j; bg.z = k; bg.step = 0;} 155 else if(matrix[i][j][k] == 'E'){ed.x = i; ed.y = j; ed.z = k;} 156 } 157 } 158 } 159 160 ans = BFS(); 161 if(ans) cout<<"Escaped in "<<ans<<" minute(s)."<<endl; 162 else cout<<"Trapped!"<<endl; 163 164 } 165 return EXIT_SUCCESS; 166 }
我曾七次鄙视自己的灵魂:
第一次,当它本可进取时,却故作谦卑;
第二次,当它在空虚时,用爱欲来填充;
第三次,在困难和容易之间,它选择了容易;
第四次,它犯了错,却借由别人也会犯错来宽慰自己;
第五次,它自由软弱,却把它认为是生命的坚韧;
第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副;
第七次,它侧身于生活的污泥中,虽不甘心,却又畏首畏尾。