标签:
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
问题分析:依旧是简单bfs,但是要用三维数组储存地图,但是有6个分支,用数组来储存方向可以让程序更简洁。
1 #include "iostream" 2 #include "queue" 3 using namespace std; 4 char maze[32][32][32]; 5 int v[6][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0}; 6 int L,R,C; 7 struct escaper 8 { 9 int i; 10 int j; 11 int k; 12 int time; 13 }; 14 escaper fir; 15 void mbegin() 16 { 17 for (int i=0;i<=L+1;i++) 18 for (int j=0;j<=R+1;j++) 19 for (int k=0;k<=C+1;k++) 20 if (i*j*k == 0 || i == L+1 || j==R+1 || k==C+1) 21 maze[i][j][k] = ‘#‘; 22 else 23 { 24 cin>>maze[i][j][k]; 25 if (maze[i][j][k] == ‘S‘) 26 { 27 fir.i = i; 28 fir.j = j; 29 fir.k = k; 30 } 31 } 32 } 33 void bfs() 34 { 35 queue <escaper> p; 36 escaper sec; 37 fir.time=0; 38 p.push(fir); 39 while (!p.empty()) 40 { 41 sec = p.front(); 42 p.pop(); 43 for (int i=0;i<6;i++) 44 { 45 fir.i = sec.i+v[i][0]; 46 fir.j = sec.j+v[i][1]; 47 fir.k = sec.k+v[i][2]; 48 if (maze[fir.i][fir.j][fir.k] != ‘#‘) 49 { 50 fir.time = sec.time+1; 51 if (maze[fir.i][fir.j][fir.k] == ‘E‘) 52 { 53 cout<<"Escaped in "<<fir.time<<" minute(s)."<<endl; 54 return; 55 } 56 maze[fir.i][fir.j][fir.k] = ‘#‘; 57 p.push(fir); 58 } 59 } 60 } 61 cout<<"Trapped!"<<endl; 62 } 63 int main() 64 { 65 while (cin>>L>>R>>C && L && R && C) 66 { 67 mbegin(); 68 bfs(); 69 } 70 return 0; 71 }
暑假集训(1)第三弹 -----Dungeon Master(Poj2251)
标签:
原文地址:http://www.cnblogs.com/huas-zlw/p/5676774.html