题目链接:http://poj.org/problem?id=2251
题目大意:给定三维迷宫,起点和终点,问能否逃出迷宫,能的话最短时间
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!
题目思路:模板题BFS,换成三维坐标了而已
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
int l,r,c;
char mp[35][35][35];
int vis[35][35][35];
int dir[6][3]={0,1,0,0,-1,0,1,0,0,-1,0,0,0,0,1,0,0,-1}; //6个方向扩展
struct node
{
int x,y,z,step;
}a,next;
bool check(int A,int B,int C)
{
if(A<=0||A>l||B<=0||B>r||C<=0||C>c||vis[A][B][C]==1)
return false;
if(mp[A][B][C]=='#')
return false;
return true;
}
int bfs()
{
queue<node>q;
q.push(a);
vis[a.x][a.y][a.z]=1;
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<6;i++)
{
next.x=a.x+dir[i][0];
next.y=a.y+dir[i][1];
next.z=a.z+dir[i][2];
next.step=a.step+1;
if(mp[next.x][next.y][next.z]=='E')
return next.step;
if(check(next.x,next.y,next.z))
{
vis[next.x][next.y][next.z]=1;
q.push(next);
}
}
}
return -1;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(~scanf("%d%d%d",&l,&r,&c))
{
if(l==0&&r==0&&c==0)
break;
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
scanf(" %c",&mp[i][j][k]);
if(mp[i][j][k]=='S')
{
a.x=i;
a.y=j;
a.z=k;
a.step=0;
}
}
}
}
MEM(vis,0);
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}