就是走迷宫 。。 在三维空间上。。
题目:
Dungeon Master |
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input Specification
The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes
one cell of the dungeon. A cell full of rock is indicated by a `#' and empty cells are represented by a
`.'. Your starting position is indicated by `S' and the exit by the letter 'E'. There's a single blank
line after each level. Input is terminated by three zeroes for L, R and C.
Output Specification
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <memory.h> 5 using namespace std; 6 7 8 9 const int maxn = 50; 10 11 12 struct node{ 13 int r,c,l; 14 }; 15 16 const int dx[]={0,0,1,-1,0,0}; 17 const int dy[]={0,0,0,0,1,-1}; 18 const int dz[]={1,-1,0,0,0,0}; 19 char G[maxn][maxn][maxn]; 20 int vis[maxn][maxn][maxn]; 21 int ans[maxn][maxn][maxn]; 22 int L,R,C; 23 int sx,ex,sy,ey,sz,ez; 24 25 inline bool check(int x,int y,int z) 26 { 27 if(x>=0&&x<R && y>=0&&y<C && z>=0&& z<L)return true; 28 else 29 return false; 30 } 31 32 int bfs() 33 { 34 35 queue<node> v; 36 v.push(node{sx,sy,sz}); 37 vis[sx][sy][sz]=1; 38 ans[sx][sy][sz]=0; 39 while(!v.empty()) 40 { 41 node now=v.front(); 42 int nx=now.r,ny=now.c,nz=now.l; 43 44 int step = ans[nx][ny][nz]; 45 for(int i=0;i<6;i++) 46 { 47 int ax = nx+dx[i],ay=ny+dy[i],az=nz+dz[i]; 48 if(check(ax,ay,az)&&!vis[ax][ay][az] && G[ax][ay][az]=='.'||G[ax][ay][az]=='E') 49 { 50 // cout<<ax<<" "<<ay<<" "<<az<<endl; 51 ans[ax][ay][az]=step+1; 52 if(ax==ex && ay==ey && az==ez) 53 return 0; 54 v.push(node{ax,ay,az}); 55 vis[ax][ay][az]=1; 56 } 57 } 58 v.pop(); 59 } 60 return 0; 61 62 } 63 64 65 int main() 66 { 67 freopen("in.txt","r",stdin); 68 69 while(cin>>L>>R>>C &&L !=0) 70 { 71 getchar(); 72 for(int z=0;z<L;z++) 73 { 74 for(int x=0;x<R;x++) 75 { 76 for(int y=0;y<C;y++) 77 { 78 G[x][y][z]=getchar(); 79 if(G[x][y][z]=='S'){sx=x;sy=y;sz=z;} 80 if(G[x][y][z]=='E'){ex=x;ey=y;ez=z;} 81 82 } 83 getchar(); 84 } 85 getchar(); 86 } 87 bfs(); 88 if(ans[ex][ey][ez]) 89 cout<<"Escaped in "<<ans[ex][ey][ez]<<" minute(s)."<<endl; 90 else 91 cout<<"Trapped!"<<endl; 92 memset(ans,0,sizeof(ans)); 93 memset(vis,0,sizeof(vis)); 94 memset(G,0,sizeof(G)); 95 96 97 } 98 99 return 0; 100 101 }