Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27195 | Accepted: 10668 |
Description
Is an escape possible? If yes, how long will it take?
Input
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
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
分析:稍微轉換一下即可。沒什麼東西感覺,給出AC代碼:
//三維bfs
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=31;
int map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
int c,a,b;
int dirx[6]= {-1,1,0,0,0,0};
int diry[6]= {0,0,-1,1,0,0};
int dirz[6]= {0,0,0,0,-1,1};
struct node
{
int x,y,z,step;
};
int BFS()
{
queue<node>q;
node p1;
p1.x=sx;
p1.y=sy;
p1.z=sz;
vis[sx][sy][sz]=1;
p1.step=0;
q.push(p1);
//printf("%d %d %d\n",p1.x,p1.y,p1.z);
while(!q.empty())
{
node f=q.front(),temp;
q.pop();
if(f.x==ex&&f.y==ey&&f.z==ez)
return f.step;
for(int i=0; i<6; i++)
{
temp.x=f.x+dirx[i];
temp.y=f.y+diry[i];
temp.z=f.z+dirz[i];
temp.step=f.step+1;
if(temp.x<a&&temp.x>=0&&temp.y<b&&temp.y>=0&&temp.z<c&&temp.z>=0&&
!vis[temp.x][temp.y][temp.z]&&(map[temp.x][temp.y][temp.z]==1))
{
q.push(temp);
// printf("%d %d %d\n",temp.x,temp.y,temp.z);
vis[temp.x][temp.y][temp.z]=1;
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&c,&a,&b),a,b,c)
{
char temp;
for(int i=0; i<c; i++)//平面座標
for(int j=0; j<a; j++)//j是平面x
for(int k=0; k<b; k++)//k是平面y
{
vis[j][k][i]=0;
cin>>temp;
if(temp=='.')
map[j][k][i]=1;
else if(temp=='#')
map[j][k][i]=0;
else if(temp=='S')
{
sx=j;
sy=k;
sz=i;
map[j][k][i]=1;
}
else if(temp=='E')
{
ex=j;
ey=k;
ez=i;
map[j][k][i]=1;
}
}
int res=BFS();
if(res==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",res);
}
return 0;
}
特記下,以備後日回顧。