Dungeon Master(bfs)

来源:互联网 发布:linux内核源代码 编辑:程序博客网 时间:2024/05/17 22:59
E - Dungeon Master
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
SubmitStatus Practice POJ 2251
Appoint description:

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!
题意:
   给你一个层数和行数,列数。最后要你找出是否存在一条最短路到终点。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<string>#include<cstring>#include<queue>using namespace std;typedef unsigned long long ll;#define T 35int c,n,m,cnt,pos;char map[T][T][T];struct node{int x,y,z;int c;}a,b;queue<node> t;int fx[][3] = {{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{1,0,0},{-1,0,0}};bool jugde(int z,int x,int y){if(z>=0&&z<c&&x>=0&&x<n&&y>=0&&y<m){return true;}return false;}int bfs(int z,int x,int y){int i;map[z][x][y] = '#';a.z = z,a.x = x,a.y = y;a.c = 0;t.push(a);while(!t.empty()){a = t.front(),t.pop();for(i = 0;i<6;++i){b.x=a.x+fx[i][1],b.y=a.y+fx[i][2],b.z=a.z+fx[i][0];if(map[b.z][b.x][b.y]!='#'&&jugde(b.z,b.x,b.y)){b.c = a.c + 1;if(map[b.z][b.x][b.y]=='E'){return b.c;}map[b.z][b.x][b.y] = '#';t.push(b);}}}return -1;}int main(){    /*freopen("input.txt","r",stdin);*/int i,j,k,tx,ty,tz;while(scanf("%d%d%d",&c,&n,&m)&&(n||m||c)){for(i=0;i<c;++i){for(j=0;j<n;++j){for(k=0;k<m;++k){scanf(" %c",&map[i][j][k]);if(map[i][j][k]=='S'){tz = i,tx = j,ty = k;}}}}k = bfs(tz,tx,ty);if(k!=-1){printf("Escaped in %d minute(s).\n",k);}else{printf("Trapped!\n");}while(!t.empty()){t.pop();}}    return 0;}

0 0