链接:
http://poj.org/problem?id=2251http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#problem/E
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14103 | Accepted: 5477 |
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 formEscaped 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
Ulm Local 1997code:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
const int maxn = 40;
int map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
char str[maxn];
int L,R,C;
int flag;
int step;
int dir[6][3] = {1,0,0,
0,0,1, 0,1,0, 0,0,-1, 0,-1,0,
-1,0,0};
struct Point{
int x,y,z;
int step;
}st,en;
void bfs(int x, int y, int z)
{
queue<Point> q;
while(!q.empty()) q.pop(); //清空队列
Point now,next;
now.x = x; now.y = y; now.z = z; now.step = 0;
q.push(now); //起点入队
memset(vis, 0, sizeof(vis));
vis[x][y][z] = 1;
while(!q.empty())
{
now = q.front(); q.pop();
if(now.x == en.x && now.y == en.y && now.z == en.z)
{
flag = 1; //标记找到
en.step = now.step; //
return;
}
for(int i = 0; i < 6; i++)
{
next.x = now.x+dir[i][0];
next.y = now.y+dir[i][1];
next.z = now.z+dir[i][2];
if(map[next.x][next.y][next.z] && !vis[next.x][next.y][next.z])
{
vis[next.x][next.y][next.z] = 1;
next.step = now.step+1;
q.push(next);
if(next.x == en.x && next.y == en.y && next.z == en.z)
{
flag = 1;
en.step = next.step;
return;
}
}
}
}
return;
}
int main()
{
while(scanf("%d%d%d", &L,&R,&C) != EOF)
{
if(L == 0 && R == 0 && C == 0) break;
memset(map,0,sizeof(map));
gets(str);
char c;
for(int i = 1; i <= L; i++)
{
for(int j = 1; j <= R; j++)
{
for(int k = 1; k <= C; k++)
{
scanf("%c", &c);
if(c == 'S')
{
st.x = i; st.y = j; st.z = k;
}
if(c == 'E')
{
en.x = i; en.y = j; en.z = k;
}
if(c != '#') map[i][j][k] = 1; //标记可以走标记为 1,避免了判断边界问题
}
gets(str);
}
gets(str);
}
flag = 0; //初始化
bfs(st.x, st.y, st.z);
if(flag == 0) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", en.step);
}
return 0;
}