本文已参与「新人创作礼」活动,一起开启掘金创作之路。 [](2251 -- Dungeon Master (poj.org))
| Time Limit: 1000MS | Memory Limit: 65536K | |
|---|---|---|
| Total Submissions: 87754 | Accepted: 30751 |
DescriptionYou 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?InputThe 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.OutputEach 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! Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题意:给出a,b,c,表示一个三维的一个地牢,起始点‘S’,终点‘E’,若能成功逃出,输出最小步数,否则输出Trapped..被陷入困境,六个方向可以移动,分别是东南西北上下,‘#’表示石头,‘.’表示可以走..
分析:用队列从起点开始广搜,直到找到一条逃出路线结束
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<queue>
char s[50][50][50];
int book[50][50][50];
int a,b,c,f[6][3]= {1,0,0,0,0,1,0,0,-1,-1,0,0,0,1,0,0,-1,0};
struct node {
int x,y,z,step;
};
void bfs(int x,int y,int z) {
int dx,dy,dz;
queue<node>q;
node u,v;
u.x=x,u.y=y,u.z=z,u.step=0;
book[u.x][u.y][u.z]=1;
q.push(u);
while(!q.empty()) {
u=q.front();
q.pop();
if(s[u.x][u.y][u.z]=='E') {
printf("Escaped in %d minute(s).\n",u.step);
return ;
}
for(int i=0; i<6; i++) {
dx=u.x+f[i][0],dy=u.y+f[i][1],dz=u.z+f[i][2];
if(dx<0||dy<0||dz<0||dx>=a||dy>=b||dz>=c||s[dx][dy][dz]=='#'||book[dx][dy][dz]==1)
continue;
book[dx][dy][dz]=1;
v.x=dx,v.y=dy,v.z=dz,v.step=u.step+1;
q.push(v);
}
}
printf("Trapped!\n");
}
int main() {
while(~scanf("%d %d %d",&a,&b,&c)&&(a+b+c)) {
memset(book,0,sizeof(book));
for(int i=0; i<a; i++)
for(int j=0; j<b; j++)
scanf("%s",s[i][j]);
for(int i=0; i<a; i++) {
for(int j=0; j<b; j++) {
for(int k=0; k<c; k++) {
if(s[i][j][k]=='S')
bfs(i,j,k);
}
}
}
}
return 0;
}