poj 2251 (bfs)

来源:互联网 发布:阿里云绑定域名教程 编辑:程序博客网 时间:2024/05/21 10:43

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16206 Accepted: 6280

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!

Source

Ulm Local 1997

#include<iostream>#include<cstdio>#include<cstring> #include<queue>#define maxm 35using namespace std;int n, m, t;char map[maxm][maxm][maxm];bool visit[maxm][maxm][maxm];int dir[6][3] ={0,-1,0,0,1,0,0,0,-1,0,0,1,1,0,0,-1,0,0};int bx, by, bz;int ex, ey, ez;struct point{int x, y, z, step;};bool check(int x, int y, int z){if(x>=1&&x<=n&&y>=1&&y<=m&&z>=1&&z<=t)if(map[z][x][y]!='#'&&!visit[z][x][y]){return true;}return false;}int bfs(){queue<point> q;point cur, next;cur.x = bx;cur.y = by;cur.z = bz;cur.step = 0;q.push(cur);visit[bz][bx][by]=1;while(!q.empty()){cur = q.front();q.pop();int x, y, z;for(int i = 0; i < 6; i++){z = cur.z + dir[i][0];x = cur.x+dir[i][1];y = cur.y+dir[i][2];if(check(x,y, z)){//满足进一步宽搜的条件 next.x = x;next.y = y;next.z = z;next.step = cur.step+1;if(x==ex&&y==ey&&z==ez){//达到目的地 return next.step;}visit[z][x][y] = 1;q.push(next);}}}return -1;}int main(){char c;while(cin>>t>>n>>m,t||n||m){for(int k = 1; k <= t; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin>>map[k][i][j];if(map[k][i][j]=='S'){bx = i;by = j; bz = k;}else if(map[k][i][j]=='E'){ex = i; ey = j; ez = k;}}}getchar();}int ans = bfs();if(ans!=-1)printf("Escaped in %d minute(s).\n", ans);elseprintf("Trapped!\n");memset(visit, 0, sizeof(visit));}return 0;}


0 0
原创粉丝点击