【题目描述】
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!
题目大意是给你一个立体的空间,可以沿着‘.’走,问你能不能从‘S’ 走到 ‘E’。简单的bfs,和二维空间是一样的,只不过把四个方向变成了六个方向。
我原来是用dfs写的,结果超时,其实我认为这两个方式在时间复杂度上应当没有太大的区别。应该是我没有优化好。
dfs的代码我也没有删,只是没有调用
【代码】
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#define N 50
using namespace std;
struct T{
int x;
int y;
int z;
int de;
};
int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {-1,1,0,0,0,0};
int dz[6] = {0,0,1,-1,0,0};
int f[N][N][N];
int g[N][N][N];
int a[N][N][N];
int n,m,k;
int i,j,l;
int zx,zy,zz,sx,sy,sz;
int ans;
void dfs(int x,int y,int z,int de){
if (f[x][y][z] == 0) f[x][y][z] = N*N*N*N;
if(de >= f[x][y][z]) return;
f[x][y][z] = de;
if (x == zx && y == zy && z == zz){
ans = de;
}
for(int i = 0;i <= 6;i++){
int xx = x + dx[i];
int yy = y + dy[i];
int zz = z + dz[i];
if (xx<1 || xx>n || yy<1 || yy>m || zz<1 ||zz>k) continue;
if (a[xx][yy][zz] == 0 || g[xx][yy][zz] == 1) continue;
g[xx][yy][zz] = 1;
dfs(xx,yy,zz,de+1);
g[xx][yy][zz] = 0;
}
}
int bfs(int x, int y, int z){
queue<T> q;
{
T a;
a.x = x;
a.y = y;
a.z = z;
a.de = 0;
q.push(a);
}
while(!q.empty()){
T b = q.front();
T c;
for(int i = 0;i<6;i++){
int xx = b.x + dx[i];
int yy = b.y + dy[i];
int tz = b.z + dz[i];
if(xx == zx && yy == zy && tz == zz) {
cout<<"Escaped in "<<(b.de + 1)<<" minute(s)."<<endl;
return 1;
}
if (xx<1 || xx>n || yy<1 || yy>m || tz<1 ||tz>k) continue;
if (a[xx][yy][tz] == 0 || g[xx][yy][tz] == 1) continue;
g[xx][yy][tz] = 1;
c.x = xx;c.y = yy;c.z = tz;c.de = b.de + 1;
q.push(c);
}
q.pop();
}
return 0;
}
int main()
{
string s;
while(cin>>n>>m>>k && n && m && k){
ans = 0;
memset(a,0,sizeof(a));
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin>>s;
for(int l = 0;l < k;l++){
if (s[l] != '#') a[i][j][l+1] = 1;
if (s[l] == 'S') {
sx = i;
sy = j;
sz = l+1;
}
if (s[l] == 'E'){
zx = i;
zy = j;
zz = l+1;
}
}
}
}
g[sx][sy][sz] = 1;
// dfs(sx,sy,sz,0);
if (bfs(sx,sy,sz) == 0) cout<<"Trapped!"<<endl;
//else cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}