poj--2251

来源:互联网 发布:网络专接本 编辑:程序博客网 时间:2024/06/08 11:03
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22581 Accepted: 8814

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).

Trapped!

解体思路:超时了两遍,原来是走过的路进行标记时写成了 == 的判断语句。简单的三维bfs,把4个方向改为6个方向就行了。

代码如下:

#include<stdio.h>#include<queue>#define INF 0x3f3f3f3fusing namespace std;struct stu{int z,x,y,t;};stu edge[33*33*33];char mark[32][32][32];int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};int ans;int l,r,c;bool check(stu temp){if(temp.z<0||temp.x<0||temp.y<0||temp.z>=l||temp.x>=r||temp.y>=c)return false;if(mark[temp.z][temp.x][temp.y]=='#')return false;return true;}void bfs(int z,int x,int y){queue<stu>q;while(!q.empty()){q.pop();}stu temp,star;temp.z=z;temp.x=x;temp.y=y;temp.t=0;q.push(temp);while(!q.empty()){star=q.front();q.pop();for(int i=1;i<=6;i++){//定义6个方向   temp.x = star.x+dis[i][0];  temp.y = star.y+dis[i][1];  temp.z =star.z+dis[i][2];  temp.t=star.t +1;if(check(temp)){if(mark[temp.z][temp.x][temp.y]=='E'){ans=temp.t;return ;}q.push(temp);mark[temp.z][temp.x][temp.y]='#';} }}}int main(){int z,x,y;while(scanf("%d%d%d",&l,&r,&c)){if(l==0&&r==0&&c==0)break;for(int k=0;k<l;k++){if(k>0)getchar();for(int i=0;i<r;i++){getchar();for(int j=0;j<c;j++){scanf("%c",&mark[k][i][j]);if(mark[k][i][j]=='S'){z=k;x=i;y=j;}}}}    ans=INF;    mark[z][x][y]='#';bfs(z,x,y);    if(ans>=INF){    printf("Trapped!\n");    }    else printf("Escaped in %d minute(s).\n",ans);}return 0;}


0 0
原创粉丝点击