Dungeon Master
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 |
题目大意:
有一个空间,为 L 层,R 行,C 列;# 表示墙, ” . “ 表示可走,S为起点, E 为终点,每走一步花费一个单位的时间,求从起点到重点的最短时间x,
输出 Escaped in x minute(s). 如果不能走出来, 输出 Trapped!
思路:
求最短路径,很显然要用广搜,只不过这个是三维的空间,多了两个搜索方向;
Memory: 956K Time: 47MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
struct node{
int x, y, z, step;
};
/*程序中的x y z 并不是按照空间直角坐标系来定义的,而是跟主函数一致*/
int L, R, C;
char mp[35][35][35];
int vis[35][35][35];
int dicy[8] = {0,0,-1,1,0,0};
int dicx[8] = {1,-1,0,0,0,0};
int dicz[8] = {0,0,0,0,1,-1};
bool check_out(int x, int y, int z){
if( x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C&&!vis[x][y][z]&&mp[x][y][z]!='#')
return true;
return false;
}
int bfs( int x, int y, int z )
{
struct node p;
p.x = x;
p.y = y;
p.z = z;
queue<struct node> q;
q.push(p);
while(!q.empty())
{
p = q.front();
q.pop();
if( mp[p.x][p.y][p.z]=='E' )
return p.step;
for( int i = 0;i < 6;i++ )
{
int dx, dy, dz;
dx = p.x + dicx[i];
dy = p.y + dicy[i];
dz = p.z + dicz[i];
if( check_out(dx, dy, dz) )
{
vis[dx][dy][dz] = 1;
struct node t;
t.x = dx;
t.y = dy;
t.z = dz;
t.step = p.step+1;
q.push(t);
}
}
}
return -1;
}
int main()
{
int x, y, z;
while( cin>>L>>R>>C ){
if( L==0&&R==0&&C==0 )
break;
memset(vis, 0, sizeof(vis));
for( int i = 0;i < L;i++ ){
for( int j = 0;j < R;j++ ){
for( int k = 0;k < C;k++ ){
cin>>mp[i][j][k];
if( mp[i][j][k]=='S' ){
x = i;
y = j;
z = k;
}
}
getchar();
}
//getchar();
}
int step;
step = bfs(x,y,z);
if( step==-1 )
cout<<"Trapped!"<<endl;
else
printf("Escaped in %d minute(s).\n",step);
}
return 0;
}