Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14820 | Accepted: 5752 |
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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
初学,好多问题并不是很清楚,算是自主AC的第一道题
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
class maze{
public:
char mat[30][30];
}M[30];
class node{
public:
int x,y,z;
bool operator == (node &b){
return (x==b.x && y==b.y && z==b.z);
}
};
int dir[6][3]={
{-1, 0, 0},
{1, 0, 0},
{0, 1, 0},
{0, -1, 0},
{0, 0, 1},
{0, 0, -1}
};
int vis[30][30][30];
node s,e;
int bfs(int l, int r, int c){
int cnt = 0;
memset(vis,0,sizeof(vis));
queue<node> Q;
int Q_size;
node head,next;
Q.push(s);
vis[s.x][s.y][s.z] = 1;
while(!Q.empty()){
Q_size = Q.size();
cnt++;
while(Q_size--){
head = Q.front();
Q.pop();
if(head==e) return cnt-1;
for(int i=0; i<6; i++){
next.x = head.x + dir[i][0];
next.y = head.y + dir[i][1];
next.z = head.z + dir[i][2];
if(dir[i][2]>0 && next.z<l-1){
if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1 ||
next.z<0 || next.z>l-1 || M[next.z].mat[next.x][next.y]=='#'
|| vis[next.x][next.y][next.z]) continue;
vis[next.x][next.y][next.z] = cnt;
Q.push(next);
}else if(dir[i][2]<0 && next.z>0){
if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1 ||
next.z<0 || next.z>l-1 || M[next.z].mat[next.x][next.y]=='#'
|| vis[next.x][next.y][next.z]) continue;
vis[next.x][next.y][next.z] = cnt;
Q.push(next);
}else{
if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1 ||
next.z<0 || next.z>l-1 || M[next.z].mat[next.x][next.y]=='#'
|| vis[next.x][next.y][next.z]) continue;
vis[next.x][next.y][next.z] = cnt;
Q.push(next);
}
}
}
}
return 0;
}
int main()
{
int c,l,r;
while(cin>>l>>r>>c){
if(l==0 && r==0 && c==0) break;
for(int i=0; i<l; i++){
for(int j=0; j<r; j++){
for(int k=0; k<c; k++){
cin>>M[i].mat[j][k];
if(M[i].mat[j][k]=='S'){
s.x = j; s.y = k; s.z = i;
}else if(M[i].mat[j][k]=='E'){
e.x = j; e.y = k; e.z = i;
}
}
}
}
int ok = bfs(l,r,c);
if(ok) cout<<"Escaped in "<<ok<<" minute(s)."<<endl;
else cout<<"Trapped!"<<endl;
}
return 0;
}
再贴个别人写的
#include<iostream>
#include <queue>
using namespace std;
#define MAX 30
int L,R,C,possible,steps,visit[MAX][MAX][MAX];
char dungeon[MAX][MAX][MAX];
int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct point
{
int x,y,z;
};
point start,end;
void init() //initialization
{
int i,j,k;
steps=0;
for(i=0;i<L;i++)
for(j=0;j<R;j++)
for(k=0;k<C;k++)
{
cin>>dungeon[i][j][k];
if(dungeon[i][j][k]=='S')
{
start.x=i; start.y=j; start.z=k;
}
else if(dungeon[i][j][k]=='E')
{
end.x=i; end.y=j; end.z=k;
}
}
}
void bfs()
{
int que_size,i;
point head,next;
queue<point>que;
possible=0;
memset(visit,0,sizeof(visit));
visit[start.x][start.y][start.z]=1;
que.push(start);
while(!que.empty())
{
que_size=que.size();
while(que_size--)
{
head=que.front();
que.pop();
if(head.x==end.x&&head.y==end.y&&head.z==end.z)
{
possible=1;
return;
}
for(i=0;i<6;i++)
{
next.x=head.x+dir[i][0]; next.y=head.y+dir[i][1]; next.z=head.z+dir[i][2];
if(next.x<0||next.x>=L||next.y<0||next.y>=R||next.z<0||next.z>=C||dungeon[next.x][next.y][next.z]=='#') continue;
if(!visit[next.x][next.y][next.z])
{
visit[next.x][next.y][next.z]=1;
que.push(next);
}
}
}
steps++;
}
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)&&L)
{
init();
bfs();
if(possible) printf("Escaped in %d minute(s).\n",steps);
else printf("Trapped!\n");
}
return 0;
}