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!
http://poj.org/problem?id=2251
大意:
你被困在三维空间中,给你长宽高和地图,问最短多长时间能出去
tip:
最短,bfs
相比裸bfs,出现了三维,如何处理?
vis变成三维,意味着空间中同一个点不会出现多次
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define maxn 35
using namespace std;
typedef pair<int,pair<int,int> >pii;
#define P(a,b,c) make_pair(a,make_pair(b,c))
int c,n,m,sc,sr,sl,ec,el,er,step[maxn][maxn][maxn];
int dir[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
bool vis[maxn][maxn][maxn];
char s[maxn][maxn][maxn];
void init(){
memset(vis,false,sizeof(vis));
for(int i = 0 ; i < c ;i++)
for(int j = 0; j < n ;j++){
scanf("%s",s[i][j]);
for(int k = 0; k < m ;k++){
if(s[i][j][k] == 'S') sc=i,sr=j,sl=k;
if(s[i][j][k] == 'E') ec=i,er=j,el=k;
}
}
}
void sov(){
queue<pii>q;
q.push(P(sc,sr,sl));
vis[sc][sr][sl] = true;
step[sc][sr][sl] = 0;
while(!q.empty()){
pii tmp = q.front();
q.pop();
if(tmp.first == ec&&tmp.second.first==er&&tmp.second.second==el){
printf("Escaped in %d minute(s).\n",step[ec][er][el]);
return;
}
for(int i =0 ; i < 6 ;i++){
int nc = tmp.first+dir[i][0],nx=tmp.second.first+dir[i][1],ny=tmp.second.second+dir[i][2];
if(nc>=0&&nc<c&&nx>=0&&nx<n&&ny>=0&&ny<m)
if(s[nc][nx][ny] != '#'&&vis[nc][nx][ny]==false){
vis[nc][nx][ny] = true;
q.push(P(nc,nx,ny));
step[nc][nx][ny] = step[tmp.first][tmp.second.first][tmp.second.second]+1;
}
}
}
printf("Trapped!\n");
return;
}
int main(){
while(~scanf("%d%d%d",&c,&n,&m)&&n){
init();
sov();
}
}