POJ2251-Dungeon Master
题意:给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间。不同L层的地图,相同RC坐标处是连通的,“#”不可通过,“.”可走。
分析:最短路Bfs,和二维的基本一样,就是原来4个方向,现在6个方向,原来数组是二维,现在是三维,也相当于模板题了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; char map[][][];
int vis[][][];
int k,n,m,sx,sy,sz,ex,ey,ez;
int dx[]={-,,,,,};
int dy[]={,,-,,,};
int dz[]={,,,,,-};
struct node
{
int x,y,z,step;
}; int check(int x,int y,int z)
{
if(x<||y<||z<||x>=k||y>=n||z>=m||map[x][y][z]=='#'||vis[x][y][z])
return ;
return ;
} int bfs()
{
node a,next;
queue<node> Q;
a.x=sx;
a.y=sy;
a.z=sz;
a.step=;
vis[sx][sy][sz]=;
Q.push(a);
while(!Q.empty())
{
a=Q.front();
Q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
return a.step;
for(int i=;i<;++i)
{
next=a;
next.x=a.x+dx[i];
next.y=a.y+dy[i];
next.z = a.z+dz[i];
if(check(next.x,next.y,next.z))
continue;
vis[next.x][next.y][next.z]=;
next.step=a.step+;
Q.push(next);
}
}
return ;
} int main()
{
while(scanf("%d%d%d",&k,&n,&m),n+m+k)
{
for(int i=;i<k;++i)
{
for(int j=;j<n;++j)
{
scanf("%s",map[i][j]);
for(int r=;r<m;++r)
{
if(map[i][j][r]=='S')
{
sx=i;
sy=j;
sz=r;
}
else if(map[i][j][r]=='E')
{
ex=i;
ey=j;
ez=r;
}
}
}
}
memset(vis,,sizeof(vis));
int ans;
ans=bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
POJ2251-Dungeon Master的更多相关文章
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 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 ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- POJ2251——Dungeon Master(三维BFS)
和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向. 用上队列的进出操作较为轻松. #include<iostream> #include& ...
- bfs—Dungeon Master—poj2251
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32228 Accepted: 12378 ...
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- 自学ConcuurentHashMap源码
自学ConcuurentHashMap源码 参考:https://my.oschina.net/hosee/blog/675884 http://www.cnblogs.com/ITtangtang/ ...
- asp.net 第三方UI控件 Telerik KendoUI 之 TreeVIew 的用法记录
一.前台显示 备注:一次性取出所有节点 function loadTreeData() { $.ajax({ type: 'POST', url: '@(Html.UrlHref("Scri ...
- iOS之 Category 属性 的理解
在 Objective-C 中可以通过 Category 给一个现有的类添加属性,但是却不能添加实例变量 反正读第一遍的时候我是有点晕的,可以添加“属性”,然后又说“添加实例变量”,第一感觉就好像 有 ...
- C#中判断语句 if、if-else if、switch-case
1.if一般用于一个条件的判断: 2.if-else if 一般用于多个条件的判断: 3.switch-case一般用于多个条件的判断. 注:if-else if与switch-case的区别在于:一 ...
- ubuntu 常用命令集
一.安装的时候,让你输入代替root用户的名称与密码 使用sudo root切换root的时候会要求你输入密码,这时候你输入什么都不对的 要想使用的哈,需要给root设置密码,命令如下: sudo p ...
- 对抗栈帧地址随机化/ASLR的两种思路和一些技巧
栈帧地址随机化是地址空间布局随机化(Address space layout randomization,ASLR)的一种,它实现了栈帧起始地址一定程度上的随机化,令攻击者难以猜测需要攻击位置的地址. ...
- C++ 随机生成一个(0,1)之间的小数
double p; ]; memset(s,,sizeof(s)); s[]='; s[]='.'; ;i<;i++) { s[i]=rand()%+'; } p=atof(s); cout & ...
- HDU1166 敌兵布阵(线段树)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- SeaJS之shim插件:解决非cmd规范的插件与sea的区别
SeaJS 中的模块默认都遵守 CMD 规范,但现实中已存在大量普通 JavaScript 类库,比如 jQuery.Underscore 等.使用 shim 插件,可以将这些普通 JS 文件转换成 ...
- EOJ 3242 重复数
重复数 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: 256 megabytes 有 ...