Dungeon Master bfs
Description
Is an escape possible? If yes, how long will it take?
Input
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
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的更多相关文章
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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 ...
- [poj] Dungeon Master bfs
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- E - Dungeon Master BFS
[NWUACM] 你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成你每次向上下前后左右移动一个单位需要一分钟你不能对角线移动并且四周封闭是否存在逃出生天的可能性?如果存在,则需要 ...
随机推荐
- 【py】安装ipython-notebook
os:ubunutu(debian)-based linux 分两步: 安装ipython及其依赖包 sudo apt-get install ipython-notebook 安装可选的附加工具 ...
- Java编程思想重点
1. Java中的多态性理解(注意与C++区分) Java中除了static方法和final方法(private方法本质上属于final方法,因为不能被子类访问)之外,其它所有的方法都是动态绑定,这意 ...
- YUI3 CSS
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Linux, Mac下Shell 数组 Array 的修理工
我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...
- Dijkstra 算法、Kruskal 算法、Prim算法、floyd算法
1.dijkstra算法 算最短路径的,算法解决的是有向图中单个源点到其他顶点的最短路径问题. 初始化n*n的数组. 2.kruskal算法 算最小生成树的,按权值加入 3.Prim算法 类似dijk ...
- WKWebView与Js实战(OC版)
前言 上一篇专门讲解了WKWebView相关的所有类.代理的所有API.那么本篇讲些什么呢?当然是实战了! 本篇文章教大家如何使用WKWebView去实现常用的一些API操作.当然,也会有如何与JS交 ...
- Linux下Date命令的用法
转自http://blog.chinaunix.net/uid-8223172-id-2511672.html linux下date的用法比较复杂,但是也用的比较多,尤其是shell里面.现总结一下自 ...
- div使用jqueryui 源码 | gridview多个功能的源码
div使用jqueryui 源码 | gridview多个功能的源码 一.gridview 选中行 改变颜色,双击选中 改变颜色 protected void gv1_SelectedIndexCha ...
- 如何扩大ImageView的点击区域
我们在开发中会遇到,给imageview设置点击事件,但是美工切的原始图片一般在24dp左右,这个尺寸点击时会出现不灵敏的情况(点击区域太小). 解决方案: 给imageView设置 scaleTy ...
- Serializable接口使用纪实
这两天依领导要求使用sonar工具测试了一下项目代码,其中有一个问题是 而这个类的结构大概是这样的: public class Demo<T> implements Serializabl ...