您的位置:首页 > 其它

POJ 2251 Dungeon Master

2018-02-06 15:08 369 查看
Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41397 Accepted: 15692
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!

简单bfs搜索题。注意每次记得清空used数组(贡献了一次wa)
//#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 50;
bool used[maxn][maxn][maxn];
int perm[maxn];
char maze[maxn][maxn][maxn];
long long n,k,cnt = 0;
long long ans = 0;
int l,r,C;
typedef struct point{
int x,y,z;
int t;
}P;
void dfs(int layer)
{
}
int dx[] = {-1,1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {0,0,0,0,-1,1};
int bfs(int a,int b,int c)
{
queue<P> T;
P cur,next;
cur.x = a;
cur.y = b;
cur.z = c;
cur.t = 0;
used[a][b][c] = true;
T.push(cur);
while(T.size())
{
cur = T.front();
T.pop();
if(maze[cur.x][cur.y][cur.z] == 'E')
return cur.t;
for(int i = 0; i < 6; i++)
{
int px = dx[i]+cur.x, py = dy[i]+cur.y, pz = dz[i]+cur.z;
if(px >= 0 && px < l && py >= 0 && py < r && pz >= 0 && pz < C)
if(maze[px][py][pz] != '#' && used[px][py][pz] == false)
{//一开始智障了没有把终点加入到队列中。。。。
next.x = px;
next.y = py;
next.z = pz;
next.t = cur.t + 1;
used[px][py][pz] = true;
T.push(next);
}
}

}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&C)
9a98
)
{
memset(used,0,sizeof(used));
memset(used,0,sizeof(maze));
if(l==0&&r==0&&C==0) break;
for(int i = 0; i < l; i++)
for(int j = 0; j < r; j++)
scanf("%s",maze[i][j]);
int flag = 1;
for(int i = 0; i < l && flag; i++)
for(int j = 0; j < r && flag; j++)
for(int k = 0; k < C; k++)
if(maze[i][j][k]=='S')
{
flag = 0;
int ans = bfs(i,j,k);
if(ans==-1) puts("Trapped!");
else printf("Escaped in %d minute(s).\n",ans);
break;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: