Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51038 Accepted: 19132 题目链接
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 lineTrapped!
Sample Input
3 4 5 S.... .###. .##.. ###.###### ##### ##.## ##...##### ##### #.### ####E1 3 3 S## #E# ###0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
【分析】
BFS+三维数组 然后用个结构体存一下坐标和深度(时间)。注意mp数组第一维是深度不要写错
//昨天晚上dfs做了下,开了三个三维数组然后内存超限....?? 然后换bfs做总是wa....
//烦躁的关上了电脑,中午做一遍就A?? 神奇....
【代码】
/*
BFS+三维
*/#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;const int maxn=35;
int dir[6][3]={
{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
char mp[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int l,r,c;struct node{int x,y,z;int dep;
}st,ed;void bfs(int x,int y,int z)
{queue<node>q;q.push(node{x,y,z,0});vis[x][y][z]=1;while(!q.empty()){node now=q.front();q.pop();for(int i=0;i<6;++i){int xx=now.x+dir[i][0];int yy=now.y+dir[i][1];int zz=now.z+dir[i][2];if(xx<0 || xx>=r || yy<0 || yy>=c || zz<0 || zz>=l)continue;if(vis[xx][yy][zz] || mp[zz][xx][yy]=='#')continue;vis[xx][yy][zz]=1;node n;n.x=xx,n.y=yy,n.z=zz,n.dep=now.dep+1;if(xx==ed.x && yy==ed.y && zz==ed.z){ed.dep=n.dep;break;}q.push(n);}}
}
int main()
{while(~scanf("%d%d%d",&l,&r,&c)){if(l==0 && r==0 && c==0)break;memset(vis,0,sizeof(vis));memset(mp,0,sizeof(mp));ed.dep=0;for(int i=0;i<l;++i){for(int j=0;j<r;++j){scanf("%s",mp[i][j]);for(int k=0;k<c;++k){if(mp[i][j][k]=='S')st.x=j,st.y=k,st.z=i;if(mp[i][j][k]=='E')ed.x=j,ed.y=k,ed.z=i;}}getchar();}bfs(st.x,st.y,st.z);if(ed.dep)printf("Escaped in %d minute(s).\n",ed.dep);else puts("Trapped!");}
}