1253 Dungeon Master

题目链接:

http://noi.openjudge.cn/ch0205/1253/

http://poj.org/problem?id=2251

总时间限制: 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!

题目大意与算法分析:
这题是一个三维的迷宫题目,其中用‘.’表示空地,用‘#’表示障碍物,'S'表示出发点,
'E'表示终点,求从起点到终点的最小移动次数。
解法和二维类似,只是在行动时除了东南西北移动外还多了上下。可以上下左右前后移动,
每次都只能移动到相邻的空位,每次需要花费一分钟,求从起点到终点最少需要多久。

输入有若干组测试数据,每一组数据格式如下:
首先输入z,x,y表示三维迷宫的规模,有z层,每层是x行y列。 (z,x,y都在30以内.)
然后输入z层的数据,每一层是x*y的字符数组,行内字符之间无空格。
当输入的z,x,y都为0时表示输入结束。

对每一组测试数据,输出最少需要的时间,格式如"Escaped in 11 minute(s).",每组数据的结果占一行。
假如无法到达,输出"Trapped!".

这题用BFS解,每次去队首元素,如果是终点则输出结果移动的次数,否则,从该点开始分别
向东南西北上下移动(如果可以走的话)并继续搜,如果到队列为空还没搜到解法,则说明无解。

题解可以参考:
http://www.cnblogs.com/ACShiryu/archive/2011/07/23/2114994.html
http://blog.csdn.net/libin56842/article/details/23702395

 #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std; struct obj
{
int zz,xx,yy;
int step;//到达该点的步数
}; int z,x,y;
char a[][][];
queue<struct obj> q;
struct obj start,End;
bool flag; int dz[]={,,,,,-};//上右下左前后
int dx[]={-,,,,,};
int dy[]={,,,-,,};
void BFS();//广搜,数据全部在全局变量 int main(int argc, char *argv[])
{
int i,j,k; while(scanf("%d%d%d",&z,&x,&y)!=EOF)
{
getchar();//吸收回车
if(z==&&x==&&y==) break; //输入三维迷宫
for(k=;k<z;k++)
{
for(i=;i<x;i++)
{
for(j=;j<y;j++)
{
a[k][i][j]= getchar();
if(a[k][i][j]=='E')
{
a[k][i][j]='.';
End.zz=k;
End.xx=i;
End.yy=j;
}
else if(a[k][i][j]=='S')
{
start.zz=k;
start.xx=i;
start.yy=j;
start.step=;
}
}
getchar();//吸收回车
}
getchar();//吸收回车
} flag=false;//尚未找到目的地
BFS();
if(flag==true)
printf("Escaped in %d minute(s).\n",End.step);
else printf("Trapped!\n");
}
return ;
} void BFS()//广搜,数据全部在全局变量
{
int i,tzz,txx,tyy;
struct obj temp; while(!q.empty()) q.pop();//清空队列 q.push(start);//出发点入队
while(!q.empty())
{
for(i=;i<;i++)
{
tzz=(q.front()).zz+dz[i];
txx=(q.front()).xx+dx[i];
tyy=(q.front()).yy+dy[i];
if(tzz>=&&tzz<z&&txx>=&&txx<x&&tyy>=&&tyy<y&&a[tzz][txx][tyy]=='.')
{
temp.zz=tzz;
temp.xx=txx;
temp.yy=tyy;
temp.step=(q.front()).step+;
a[tzz][txx][tyy]='#';
q.push(temp);
if(temp.zz==End.zz&&temp.xx==End.xx&&temp.yy==End.yy)
{
End.step=temp.step;
flag=true;
return ;
}
}
}
q.pop();
}
return ;
}
上一篇:2018年IOS/Android UI设计规范


下一篇:Android源码分析-消息队列和Looper