poj2251_kuagnbin带你飞专题一

时间:2022-11-15 09:22:42
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32684   Accepted: 12529

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!

Sample Input

3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Source

解题思路:只是把二维的变成三维的了,本质上不变,就增加了两个方向。
但我还是卡了好久,我刚开始只记录了上一步的位置,不往上一步的位置走,结果一直超时。
后来发现只要走过的地方就把vis数组置为1,以后就不能走到这个位置了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; struct node{
int x,y,z;
int step;
}; int l,r,c;
char maze[][][];
int vis[][][];
node start;
node endd;
int directx[]={,,,,,-};
int directy[]={-,,,,,};
int directz[]={,,-,,,}; int findd(){
queue<node> que;
memset(vis,,sizeof(vis));
start.step=;
que.push(start);
while(!que.empty()){
node now=que.front();
que.pop();
for(int i=;i<;i++){
node next=now;
next.x+=directx[i];
next.y+=directy[i];
next.z+=directz[i];
int xx=next.x;
int yy=next.y;
int zz=next.z;
if(maze[xx][yy][zz]=='#'||vis[xx][yy][zz]||!(xx>=&&xx<l&&yy>=&&yy<r&&yy>=&&zz>=&&zz<c)){
continue;
}
if(maze[xx][yy][zz]=='E'){
next.step+=;
return next.step;
}
if(maze[xx][yy][zz]=='.'){
next.step+=;
vis[xx][yy][zz]=;
que.push(next);
}
}
}
return ;
} int main()
{
while(scanf("%d %d %d",&l,&r,&c)&&(l+r+c!=)){
getchar();
for(int i=;i<l;i++,getchar()){
for(int j=;j<r;j++,getchar()){
for(int k=;k<c;k++){
scanf("%c",&maze[i][j][k]);
if(maze[i][j][k]=='S'){
start.x=i;
start.y=j;
start.z=k;
}
}
}
}
int ans=findd();
if(ans){
printf("Escaped in %d minute(s).\n",ans);
}else{
printf("Trapped!\n");
} } return ;
}