Description:
你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成,你每次向上下前后左右移动一个单位需要一分钟,你不能对角线移动并且四周封闭,是否存在逃出生天的可能性?如果存在,则需要多少时间?
Input:
输入第一行是一个数表示空间的数量。每个空间的描述的第一行为L,R和C(皆不超过30)。L表示空间的高度。R和C分别表示每层空间的行与列的大小。随后L层地牢,每层R行,每行C个字符。每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。你的起始位置在’S’,出口为’E’。每层空间后都有一个空行。L,R和C均为0时输入结束。
Output:
每个空间对应一行输出。如果可以逃生,则输出如下Escaped in x minute(s).x为最短脱离时间。
如果无法逃生,则输出如下
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点的最短步数。
思路:已入队过结点不要忘记标记,数组不要越界 。BFS每次都一层一层向外扩散。
#include"stdio.h"
#include"cstring"
#include"algorithm"
#include"iostream"
#include"queue"
using namespace std;
int l,r,c;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
bool vst[35][35][35];
char ch[20][20][20];
struct wz
{
int x,y,z,time;
};
bool cheak(wz a)
{
if(a.x<0||a.x>=l||a.y<0||a.y>=r ||a.z<0||a.z>=c || ch[a.x][a.y][a.z] == ‘#’ || vst[a.x][a.y][a.z])
{
return 0;
}
return 1;
}
void bfs(wz a)
{
queue q;
a.time=0;
vst[a.x][a.y][a.z]=1;
q.push(a);
wz now,next;
while(!q.empty())
{
now=q.front();
if(ch[now.x][now.y][now.z] == ‘E’)
{
cout<<“Escaped in “<<now.time<<” minute(s).”<<endl;
return ;
}
for(int i = 0;i < 4;i ++)
{
next.x=now.x;
next.y=now.y+dir[i][0];
next.z=now.z+dir[i][1];
next.time=now.time+1;
if(cheak(next))
{
vst[next.x][next.y][next.z]=1;
q.push(next);
}
}
next.x=now.x+1;
next.y=now.y;
next.z=now.z;
next.time=now.time+1;
if(cheak(next))
{
vst[next.x][next.y][next.z]=1;
q.push(next);
}
next.x=now.x-1;
next.y=now.y;
next.z=now.z;
next.time=now.time+1;
if(cheak(next))
{
vst[next.x][next.y][next.z]=1;
q.push(next);
}
q.pop();
}
cout<<“Trapped!”<<endl;
}


278




被折叠的 条评论
为什么被折叠?



