Description
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
The input 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
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 0Sample Output
Escaped in 11 minute(s). Trapped!Source
题目大意:这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了东南西北移动外还多了上下。 对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。 对于数据以可以这样移动 (1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5) ->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5) 共11步就可以到达终点 对于数据二明显不能到达,则输出Trapped 这题用BFS解,每次取队首元素,如果是终点则输出结果移动的次数,否则,从该点开始分别向东南西北上下移动(如果可以走的话)并继续搜,如果到队列为空还没搜到解法,则说明无解。 就是三维的bfs #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node { int x,y,z,step; }s,v; char mp[35][35][35]; int vis[35][35][35]; int dir[10][10]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; int l,r,c,sx,sy,sz,ex,ey,ez; void bfs() { queue<struct node>q; s.x=sx; s.y=sy; s.z=sz; s.step=0; q.push(s); while(!q.empty()) { v=q.front(); q.pop(); if(v.x==ex&&v.y==ey&&v.z==ez) { printf("Escaped in %d minute(s).\n",v.step); return; } for(int i=0;i<6;i++) { int xx=v.x+dir[i][0]; int yy=v.y+dir[i][1]; int zz=v.z+dir[i][2]; if(0<=xx&&xx<l&&0<=yy&&yy<r&&0<=zz&&zz<c&&!vis[xx][yy][zz]&&mp[xx][yy][zz]!='#') { s.x=xx; s.y=yy; s.z=zz; s.step=v.step+1; vis[xx][yy][zz]=1; q.push(s); } } } printf("Trapped!\n"); } int main() { while(~scanf("%d%d%d",&l,&r,&c)) { if(l==0&&r==0&&c==0) break; for(int i=0;i<l;i++) { for(int j=0;j<r;j++) { scanf("%s",mp[i][j]); } } for(int i=0;i<l;i++) { for(int j=0;j<r;j++) { for(int k=0;k<c;k++) { if(mp[i][j][k]=='S') { sx=i; sy=j; sz=k; } else if(mp[i][j][k]=='E') { ex=i; ey=j; ez=k; } } } } memset(vis,0,sizeof(vis)); vis[sx][sy][sz]=1; bfs(); } return 0; }