B - 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!
题意:有一个三维地牢,有L层,R行C列;
知道起始位置S,和E出口,问怎么样出去用的时间最少。如果出不去,就挂了。
说明1:如果可以出去,那么队列总是放不满的,直到遇见了’E’。
如果被困了,那么四面八方都出不去,队列会一点点pop()掉,最后为空就退出了。
说明2:好像没必要,还是写了吧
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
struct pos
{
int x,y,z;//记录三维坐标是的
int cont;//为记录步数的
}Stard,End;
int vis[35][35][35];
char mapp[35][35][35];
int L,R,C,flag,sumCont; //sumCont表步数
int fx[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0,}};//表示向各个方向移动
void BFS()
{
queue <pos> Q;//开一个队列
Q.push(Stard);//把起始位置放进去
vis[Stard.x][Stard.y][Stard.z]=1;
while(!Q.empty())
{
struct pos p;
p=Q.front();
Q.pop();
sumCont=p.cont;
if(mapp[p.x][p.y][p.z]=='E'){flag=1;return;}
for(int i=0;i<6;i++)
{
struct pos temp;
temp.x=p.x+fx[i][0];
temp.y=p.y+fx[i][1];
temp.z=p.z+fx[i][2];//巧妙利用数组进行方向的移动
temp.cont=sumCont+1;//步数+1
if(temp.x<1||temp.x>L||temp.y<1||temp.y>R||temp.z<1||temp.z>C) continue;//不能出界
if(mapp[temp.x][temp.y][temp.z]!='#'&&vis[temp.x][temp.y][temp.z]==0)
{
vis[temp.x][temp.y][temp.z]=1;
Q.push(temp);
}
}
}//见说明1
while(Q.empty()==0)
Q.pop();//释放掉空间,见说明2
}
int main()
{
while(scanf("%d %d %d",&L,&R,&C))
{
if(L==0&&R==0&&C==0) break;
getchar();
for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++)
{
for(int k=1;k<=C;k++)
{
cin>>mapp[i][j][k];//起先用的scanf("%c"......),太麻烦了,后来换成了这个
if(mapp[i][j][k]=='S')
{Stard.x=i;Stard.y=j;Stard.z=k;}
if(mapp[i][j][k]=='E')
{End.x=i;End.y=j;End.z=k;}//分别记录起始结束位置
}
}
}
memset(vis,0,sizeof(vis));
flag=0;sumCont=0;
BFS();
if(flag) printf("Escaped in %d minute(s).\n",sumCont);
else printf("Trapped!\n");
}
return 0;
}