标签:code 三维 empty style ons tar push ast amp
题目链接:http://poj.org/problem?id=2251
题意:三维BFS。
题解:大水题,只不过多加了两个方向
1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=0x3f3f3f3f; 9 10 int d[33][33][33]; 11 char map[33][33][33]; 12 int FX[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}; 13 struct node{ 14 int x,y,z; 15 }; 16 17 bool ok(int x,int y,int z){ 18 if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C&&map[x][y][z]!=‘#‘&&d[x][y][z]==INF) 19 return 1; 20 return 0; 21 } 22 23 void bfs(){ 24 queue <node> Q; 25 node p; 26 p.x=sx,p.y=sy,p.z=sz; 27 d[sx][sy][sz]=0; 28 Q.push(p); 29 while(!Q.empty()){ 30 node q=Q.front(); 31 Q.pop(); 32 if(q.x==ex&&q.y==ey&&q.z==ez) break; 33 for(int i=0;i<6;i++){ 34 int dx=q.x+FX[i][0]; 35 int dy=q.y+FX[i][1]; 36 int dz=q.z+FX[i][2]; 37 node TnT; 38 TnT.x=dx,TnT.y=dy,TnT.z=dz; 39 if(ok(dx,dy,dz)){ 40 Q.push(TnT); 41 d[dx][dy][dz]=d[q.x][q.y][q.z]+1; 42 } 43 } 44 } 45 } 46 47 int main(){ 48 while(cin>>L>>R>>C){ 49 if(L==0&&R==0&&C==0) break; 50 for(int i=0;i<L;i++){ 51 for(int j=0;j<R;j++){ 52 cin>>map[i][j]; 53 for(int k=0;k<C;k++){ 54 d[i][j][k]=INF; 55 if(map[i][j][k]==‘S‘) {sx=i;sy=j;sz=k;} 56 if(map[i][j][k]==‘E‘) {ex=i;ey=j;ez=k;} 57 } 58 } 59 } 60 bfs(); 61 if(d[ex][ey][ez]==INF) cout<<"Trapped!"<<endl; 62 else cout<<"Escaped in "<<d[ex][ey][ez]<<" minute(s)."<<endl; 63 } 64 65 66 return 0; 67 }
POJ - 2251 Dungeon Master(三维BFS)
标签:code 三维 empty style ons tar push ast amp
原文地址:http://www.cnblogs.com/Leonard-/p/7242189.html