标签:dia cap represent log levels ide iostream using ted
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模版题,三维bfs
1 #include<iostream> 2 #include<string.h> 3 #include<queue> 4 5 using namespace std; 6 7 int l,r,c,di,dj,dk,escape,ans,si,sj,sk; 8 char mapp[35][35][35]; 9 int flag[35][35][35]; 10 int dir[6][3]={{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}}; 11 12 struct node 13 { 14 int x,y,z; 15 int step; 16 }; 17 node qi,zhong; 18 bool judge(int x,int y,int z) 19 { 20 if(x>=0 && x<l && y>=0 && y<r && z>=0 && z<c && !flag[x][y][z] && mapp[x][y][z]!=‘#‘) 21 return true; 22 return false; 23 } 24 void bfs() 25 { 26 queue<node>que; 27 que.push(qi); 28 node a,b; 29 while(!que.empty()) 30 { 31 a = que.front(); 32 que.pop(); 33 34 if(a.x==zhong.x && a.y==zhong.y && a.z==zhong.z) 35 { 36 cout<<"Escaped in "<<a.step<<" minute(s)."<<endl; 37 return ; 38 } 39 for(int i = 0; i < 6; i++) 40 { 41 b=a; 42 b.x+=dir[i][0]; 43 b.y+=dir[i][1]; 44 b.z+=dir[i][2]; 45 if(judge(b.x,b.y,b.z)) 46 { 47 flag[b.x][b.y][b.z]=1; 48 b.step++; 49 que.push(b); 50 } 51 } 52 } 53 cout<<"Trapped!"<<endl; 54 } 55 int main() 56 { 57 while(cin>>l>>r>>c,l||r||c) 58 { 59 memset(flag,0,sizeof(flag)); 60 for(int i = 0; i < l; i++) 61 { 62 for(int j = 0; j < r; j++) 63 { 64 cin>>mapp[i][j]; 65 for(int k = 0; k < c; k++) 66 { 67 if(mapp[i][j][k]==‘S‘) 68 qi.x=i,qi.y=j,qi.z=k,qi.step=0; 69 if(mapp[i][j][k]==‘E‘) 70 zhong.x=i,zhong.y=j,zhong.z=k; 71 } 72 } 73 } 74 bfs(); 75 76 } 77 78 return 0; 79 }
[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master
标签:dia cap represent log levels ide iostream using ted
原文地址:http://www.cnblogs.com/Xycdada/p/6724510.html