描述
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?
输入
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.
输出
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!
样例输入
makrdown鬼畜了样例点击查看原题
样例输出
Escaped in 11 minute(s).
Trapped!
三维(围)迷宫问题
这份代码TLE,懒得改了直接重打
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 30+5;
char map[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int a,b,c;
struct Node{
int d,x,y,cnt;
};
Node s,t;
void bfs(Node s)
{
queue<Node> Q;
s.cnt=0;
Q.push(s);
vis[s.d][s.x][s.y]=1;
while(!Q.empty())
{
Node f=Q.front();
Q.pop();
int d=f.d;
int x=f.x;
int y=f.y;
vis[d][x][y]=1;
int cnt=f.cnt;
if(f.d==t.d&&f.x==t.x&&f.y==t.y)
{
printf("Escaped in %d minute(s).\n",cnt);
return ;
}
if(!vis[d-1][x][y]&&map[d-1][x][y]!='#'&&d>=2)
Q.push((Node){d-1,x,y,cnt+1});
if(!vis[d+1][x][y]&&map[d+1][x][y]!='#'&&d<=a-1)
Q.push((Node){d+1,x,y,cnt+1});
if(!vis[d][x-1][y]&&map[d][x-1][y]!='#'&&x>=2)
Q.push((Node){d,x-1,y,cnt+1});
if(!vis[d][x+1][y]&&map[d][x+1][y]!='#'&&x<=b-1)
Q.push((Node){d,x+1,y,cnt+1});
if(!vis[d][x][y-1]&&map[d][x][y-1]!='#'&&y>=2)
Q.push((Node){d,x,y-1,cnt+1});
if(!vis[d][x][y+1]&&map[d][x][y+1]!='#'&&y<=c-1)
Q.push((Node){d,x,y+1,cnt+1});
}
printf("Trapped!\n");
}
int main()
{
while(1)
{
memset(map,'0',sizeof(map));
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&a,&b,&c);
if(a==0&&b==0&&c==0) return 0;
for(int i=1;i<=a;i++)
for(int j=1;j<=b;j++)
for(int k=1;k<=c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
{
s.d=i;
s.x=j;
s.y=k;
}
if(map[i][j][k]=='E')
{
t.d=i;
t.x=j;
t.y=k;
}
}
bfs(s);
}
return 0;
}
下面是AC代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 30+5;
int x[7]={0,1,0,-1,0,0,0};
int y[7]={0,0,1,0,-1,0,0};
int z[7]={0,0,0,0,0,1,-1};
char a[maxn][maxn][maxn];
int b[29791],c[29791],d[29791];
int e[50000];
int main()
{
int L,R,C;
while(cin>>L>>R>>C&&L!=0&&R!=0&&C!=0)
{
int xxx,yyy,zzz;
int head=0,tail=1,f=0;
e[tail]=0;
for(int i=1;i<=L;i++)
for(int r=1;r<=R;r++)
for(int t=1;t<=C;t++)
{
cin>>a[i][r][t];
if(a[i][r][t]=='S')
{
b[tail]=i;
c[tail]=r;
d[tail]=t;
a[i][r][t]='#';
continue;
}
if(a[i][r][t]=='E')
{
xxx=i;
yyy=r;
zzz=t;
a[i][r][t]='.';
continue;
}
}
while(head!=tail)
{
head++;
for(int i=1;i<=6;i++)
{
int xx=b[head]+z[i],yy=c[head]+x[i],zz=d[head]+y[i];
if(xx>=1&&xx<=L&&yy>=1&&yy<=R&&zz>=1&&zz<=C&&a[xx][yy][zz]=='.')
{
tail++;
b[tail]=xx;
c[tail]=yy;
d[tail]=zz;
a[xx][yy][zz]='#';
e[tail]=e[head]+1;
if(xx==xxx&&yy==yyy&&zz==zzz)
{
cout<<"Escaped in "<<e[tail]<<" minute(s)."<<endl;
f=1;
break;
}
}
}
if(f==1) break;
}
if(f==1) continue;
else cout<<"Trapped!"<<endl;continue;
}
return 0;
}