谨以此篇 献给诈尸的我 和 诈尸的博客
题目
1253:Dungeon Master
总时间限制: 1000ms 内存限制: 65536kB
题目描述
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?
输入
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.
输出
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!
样例输入
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!
来源
Ulm Local 1997
题目大意
三维迷宫求最短路径
思路
广搜= =
为了纪念我一直弄混地图的长宽高,写下了这篇博客。
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 30
int R,C,H;//分别代表宽 长 高
char m[MAXN+5][MAXN+5][MAXN+5];//地图
const int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//南 北 东 西 上 下 6个方向
struct node
{
int x,y,z,st;
node(){}
node(int a,int b,int c,int d){x=a,y=b,z=c,st=d;}
bool check(){return x>0&&y>0&&z>0&&x<=C&&y<=R&&z<=H&&m[z][y][x]!='#';}
}S;
void bfs()
{
queue<node> Q;
Q.push(S);
while(!Q.empty())
{
node t=Q.front();
for(int i=0;i<6;i++)
{
node p(t.x+dir[i][0],t.y+dir[i][1],t.z+dir[i][2],t.st+1);
if(!p.check()) continue;
if(m[p.z][p.y][p.x]=='E')//就是诸如此类,需将z坐标放在前面的下标
{
printf("Escaped in %d minute(s).\n",p.st);
return;
}
m[p.z][p.y][p.x]='#';
Q.push(p);
}
Q.pop();
}
printf("Trapped!\n");
}
int main()
{
while(1)
{
scanf("%d%d%d",&H,&R,&C);
if(!H&&!R&&!C) return 0;
for(int i=1;i<=H;i++)
for(int j=1;j<=R;j++)
scanf("%s",m[i][j]+1);
for(int i=1;i<=H;i++)
for(int j=1;j<=R;j++)
for(int k=1;k<=C;k++)
if(m[i][j][k]=='S')
{
S.x=k,S.y=j,S.z=i,S.st=0;//这里也要注意
//printf(" %d %d %d\n",S.x,S.y,S.z);
break;
}
bfs();
memset(m,0,sizeof m);
}
return 0;
}