time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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! queue用法

queue 模板类的定义在<queue>头文件中。
queue 模板类需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; int main()
{
queue<int> q;
q.push();
q.push();
q.push();
printf("%d\n",q.size());
while(!q.empty())
{
int a=q.front();
q.pop();
printf("%d ",a);
}
printf("\n");
return ;
}

my daima

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF=;
int l,r,c,sl,sr,sc,el,er,ec,d[][][];
char maps[][][]; struct Node
{
int l;
int r;
int c;
}; queue<Node> q; int dr[]={,,,-,,},dc[]={,,,,,-},dl[]={,-,,,,}; int bfs()
{
int i,j,k;
for(k=;k<=l;k++)
for(i=;i<=r;i++)
for(j=;j<=c;j++)
d[k][i][j]=INF;
Node s;
s.l=sl,s.r=sr,s.c=sc;
q.push(s);
d[sl][sr][sc]=; while(q.size())
{
Node now=q.front();
q.pop();
Node nex; if(now.l==el && now.r==er && now.c==ec)
break; for(i=;i<;i++)
{
nex.l=now.l+dl[i],nex.r=now.r+dr[i],nex.c=now.c+dc[i]; if(<=nex.l && nex.l<=l && <=nex.r && nex.r<=r && <=nex.c && nex.c<=c && maps[nex.l][nex.r][nex.c]!='#' && d[nex.l][nex.r][nex.c]==INF)
{
q.push(nex);
d[nex.l][nex.r][nex.c]=d[now.l][now.r][now.c]+;
}
}
}
return d[el][er][ec];
} int main()
{
int i,j,k;
while(scanf("%d %d %d",&l,&r,&c)!=EOF)
{
getchar();
if(l== && r== && c==)
break;
for(k=;k<=l;k++)
{
for(i=;i<=r;i++)
{
for(j=;j<=c;j++)
{
scanf("%c",&maps[k][i][j]);
if(maps[k][i][j]=='S')
sl=k,sr=i,sc=j;
if(maps[k][i][j]=='E')
el=k,er=i,ec=j;
}
getchar();
}
getchar();
} int ans=bfs();
if(ans==INF)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans); /*printf("%d %d %d\n%d %d %d\n",sl,sr,sc,el,er,ec);
for(k=1;k<=l;k++)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%7d ",d[k][i][j]);
}
printf("\n");
}
printf("\n");
}*/ }
return ;
}

dsdm

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <queue> using namespace std; int l, r, c, res;
int sx, sy, sz, ex, ey, ez;
string m[][];
bool vis[][][], esp; struct Node {int x, y, z, rs;};
int dir[][] = {-,,,,,,,-,,,,,,,-,,,};
queue<Node> q; void bfs() {
Node s;
s.x = sx, s.y = sy, s.z = sz, s.rs = ;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
Node now = q.front(); q.pop();
Node tmp;
for (int i = ; i < ; ++i) {
int x = now.x+dir[i][];
int y = now.y+dir[i][];
int z = now.z+dir[i][];
if (x >= l || x < || y >= r || y < || z >= c || z < ) continue;
if (vis[z][x][y]) continue;
if (m[z][x][y] =='#') continue;
vis[z][x][y] = true;
tmp.x = x;
tmp.y = y;
tmp.z = z;
tmp.rs = now.rs +;
q.push(tmp);
if (x == ex && y == ey && z == ez) {
esp = true;
res = tmp.rs;
return ;
}
}
}
return ;
} int main() {
while(cin >> c >> l >> r) {
if (!l && !r && !c) break;
for (int i = ; i < c; ++i) {
for (int j = ; j < l; ++j) {
cin >> m[i][j];
for (int k = ; k < m[i][j].size(); ++k) {
if (m[i][j][k] == 'S') {
sx = j;
sy = k;
sz = i;
}
else if (m[i][j][k] == 'E') {
ex = j;
ey = k;
ez = i;
}
}
}
}
memset(vis, , sizeof(vis));
vis[sz][sx][sy] = true;
esp = false;
bfs();
if (esp) cout << "Escaped in "<<res<< " minute(s)." << endl;
else cout << "Trapped!" << endl;
}
return ;
}

Dungeon Master bfs的更多相关文章

  1. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  2. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  4. [poj] Dungeon Master bfs

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  5. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  6. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  7. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

  8. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  9. E - Dungeon Master BFS

    [NWUACM] 你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成你每次向上下前后左右移动一个单位需要一分钟你不能对角线移动并且四周封闭是否存在逃出生天的可能性?如果存在,则需要 ...

随机推荐

  1. linux 压缩文件的命令总结

    Linux压缩文件的读取 *.Z       compress 程序压缩的档案: *.bz2     bzip2 程序压缩的档案: *.gz      gzip 程序压缩的档案: *.tar     ...

  2. Eclipse安装插件支持jQuery智能提示

    Eclipse安装插件支持jQuery智能提示 最近工作中用到jQuery插件,需要安装eclipse插件才能支持jQuery智能提示,在网上搜索了一下,常用的有三个插件支持jQuery的智能提示:1 ...

  3. SqlServer中使用Select语句给变量赋值的时候需要注意的一个问题

    我们知道在SqlServer中可以用Select语句给变量赋值,比如如下语句就为int类型的变量@id赋值 ; select @id=id from ( as id union all as id u ...

  4. 作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的

    前日在cnblogs上看到一遍文章<每个程序员都必读的12篇文章>,其中大多数是E文的. 先译其中一篇web相关的”每个程序员必知之WEB开发”. 原文: http://programme ...

  5. c# 操作xml题目

    download! 1.新建一个文本文件,命名为:projects.txt. 2.将后缀名改为projects.xml.  3.用记事本编辑该文件.使用utf-8编码.内容如下: <?xml v ...

  6. 对EJB返回的AaaryList显示到table的处理方法

      1. ArrayList --> Object[]        ArrayList x = new ArrayList();        int i = x.size();        ...

  7. 【Pro ASP.NET MVC 3 Framework】.学习笔记.7.SportsStore:购物车

    3 创建购物车 每个商品旁边都要显示Add to cart按钮.点击按钮后,会显示客户已经选中的商品的摘要,包括总金额.在购物车里,用户可以点击继续购物按钮返回product目录.也可以点击Check ...

  8. 快速搭建Redis缓存数据库

    之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...

  9. memcache缓存的使用

    今天在,本地测试了一个关于memcache的demo. 本地集成环境(wamp)环境如下:php 5.5.12  .Apache 2.4.9 .mysql 5.6.17 1.除了添加配置.添加php的 ...

  10. 常用的math函数

    <?php    //1.abs — 绝对值                      echo abs(-77);      //ceil — 进一法取整                   ...