Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16437 | Accepted: 6386 |
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
Source
bfs过了,dfs超时了
AC代码:
#include<queue>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
char G[33][33][33];
int g[33][33][33];
struct Node{
int l,r,c;
int t;
Node(int L,int R,int C,int T):l(L),r(R),c(C),t(T){}
};
struct Node1{
int l,r,c;
}b,e;
queue <Node> qu;
int main(){
int l,r,c;
int sucess;
while(cin>>l>>r>>c){
if(l==r && l==c && c==0)
break;
for(int k=1;k<=l;k++)
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
cin>>G[k][i][j];
if(G[k][i][j]=='S'){
b.l=k; b.r=i; b.c=j;
}
else if(G[k][i][j]=='E'){
e.l=k; e.r=i; e.c=j;
}
}
while(!qu.empty())
qu.pop();
qu.push(Node(b.l,b.r,b.c,0));
sucess=0;
memset(g,0,sizeof(g));
g[b.l][b.r][b.c]=1;
while(!qu.empty()){
Node tmp=qu.front(); qu.pop();
if(tmp.l==e.l && tmp.r==e.r && tmp.c==e.c){
sucess=1;
cout<<"Escaped in "<<tmp.t<<" minute(s)."<<endl;
break;
}
if(tmp.l+1<=l && !g[tmp.l+1][tmp.r][tmp.c] && (G[tmp.l+1][tmp.r][tmp.c]=='.' || G[tmp.l+1][tmp.r][tmp.c]=='E')){ //up
qu.push(Node(tmp.l+1,tmp.r,tmp.c,tmp.t+1));
g[tmp.l+1][tmp.r][tmp.c]=1;
}
if(tmp.l-1>=1 && !g[tmp.l-1][tmp.r][tmp.c] && (G[tmp.l-1][tmp.r][tmp.c]=='.' || G[tmp.l-1][tmp.r][tmp.c]=='E')){ //down
qu.push(Node(tmp.l-1,tmp.r,tmp.c,tmp.t+1));
g[tmp.l-1][tmp.r][tmp.c]=1;
}
if(tmp.r+1<=r && !g[tmp.l][tmp.r+1][tmp.c] && (G[tmp.l][tmp.r+1][tmp.c]=='.' || G[tmp.l][tmp.r+1][tmp.c]=='E')){ //east
qu.push(Node(tmp.l,tmp.r+1,tmp.c,tmp.t+1));
g[tmp.l][tmp.r+1][tmp.c]=1;
}
if(tmp.r-1>=1 && !g[tmp.l][tmp.r-1][tmp.c] && (G[tmp.l][tmp.r-1][tmp.c]=='.' || G[tmp.l][tmp.r-1][tmp.c]=='E')){ //weat
qu.push(Node(tmp.l,tmp.r-1,tmp.c,tmp.t+1));
g[tmp.l][tmp.r-1][tmp.c]=1;
}
if(tmp.c+1<=c && !g[tmp.l][tmp.r][tmp.c+1] && (G[tmp.l][tmp.r][tmp.c+1]=='.' || G[tmp.l][tmp.r][tmp.c+1]=='E')){ //south
qu.push(Node(tmp.l,tmp.r,tmp.c+1,tmp.t+1));
g[tmp.l][tmp.r][tmp.c+1]=1;
}
if(tmp.c-1>=1 && !g[tmp.l][tmp.r][tmp.c-1] && (G[tmp.l][tmp.r][tmp.c-1]=='.' || G[tmp.l][tmp.r][tmp.c-1]=='E')){ //north
qu.push(Node(tmp.l,tmp.r,tmp.c-1,tmp.t+1));
g[tmp.l][tmp.r][tmp.c-1]=1;
}
}
if(!sucess)
cout<<"Trapped!"<<endl;
}
return 0;
}
TLE代码:
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
struct Node1{
int l,r,c;
}b;
int ans;
int l,r,c;
int sucess;
char G[33][33][33];
int g[33][33][33];
void dfs(int x,int y,int z,int t){
g[x][y][z]=1;
if(G[x][y][z]=='E'){
sucess=1;
if(t<ans){
ans=t;
}
return ;
}
if(x+1<=l && !g[x+1][y][z] && (G[x+1][y][z]=='E' || G[x+1][y][z]=='.')){
dfs(x+1,y,z,t+1);
g[x+1][y][z]=0;
}
if(x-1>=1 && !g[x-1][y][z] && (G[x-1][y][z]=='E' || G[x-1][y][z]=='.')){
dfs(x-1,y,z,t+1);
g[x-1][y][z]=0;
}
if(y+1<=r && !g[x][y+1][z] && (G[x][y+1][z]=='E' || G[x][y+1][z]=='.')){
dfs(x,y+1,z,t+1);
g[x][y+1][z]=0;
}
if(y-1>=1 && !g[x][y-1][z] && (G[x][y-1][z]=='E' || G[x][y-1][z]=='.')){
dfs(x,y-1,z,t+1);
g[x][y-1][z]=0;
}
if(z+1<=c && !g[x][y][z+1] && (G[x][y][z+1]=='E' || G[x][y][z+1]=='.')){
dfs(x,y,z+1,t+1);
g[x][y][z+1]=0;
}
if(z-1>=1 && !g[x][y][z-1] && (G[x][y][z-1]=='E' || G[x][y][z-1]=='.')){
dfs(x,y,z-1,t+1);
g[x][y][z-1]=0;
}
}
int main(){
while(cin>>l>>r>>c){
if(l==r && l==c && c==0)
break;
for(int k=1;k<=l;k++)
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
cin>>G[k][i][j];
if(G[k][i][j]=='S'){
b.l=k; b.r=i; b.c=j;
}
}
ans=999999;
sucess=0;
memset(g,0,sizeof(g));
dfs(b.l,b.r,b.c,0);
if(!sucess)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}