Dungeon Master POJ - 2251 (搜索)
|
Dungeon Master
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
where x is replaced by the shortest time it takes to escape.
Sample Input 3 4 5 Sample Output Escaped in 11 minute(s). Source |
题意:给你一个多维的迷宫,问能不能可以从起点走到终点,可以上下穿梭层数,也可以东南西北走,都是一秒一个单位,可以到终点就输出步数,不可以就输出那句话
思路:其实和普通的二维迷宫差不多,就是多了一个层数,也就是多了两个方向(上下层数)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} char map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int k,n,m,sx,sy,sz,ex,ey,ez;
int dir[][]={{,,},{,,-},{,,},{,-,},{,,},{-,,}}; 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)
return ;
else if(map[x][y][z]=='#')
return ;
else if(vis[x][y][z])
return ;
return ;
} int bfs()
{
node a,next;
queue<node>que;
a.x=sx; a.y=sy; a.z=sz;
a.step=;
vis[sx][sy][sz]=;
que.push(a);
while(!que.empty())
{
a=que.front();
que.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+dir[i][];
next.y=a.y+dir[i][];
next.z=a.z+dir[i][];
if(check(next.x,next.y,next.z))
continue;
vis[next.x][next.y][next.z]=;
next.step=a.step+;
que.push(next); }
}
return ; } int main()
{
while(~scanf("%d %d %d",&k,&n,&m))
{
if(k== && n== && m==)
break;
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 ;
}
Dungeon Master POJ - 2251 (搜索)的更多相关文章
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- Dungeon Master POJ - 2251(bfs)
对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...
- B - Dungeon Master POJ - 2251
//纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...
- 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 ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
- poj 2251 搜索
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13923 Accepted: 5424 D ...
随机推荐
- 练习九:time.sleep方法
让python程序暂停预定时间后再运行,需要用到time.sleep方法要求,随便写入一段代码,测试time.sleep方法 import time dict1 = {1:'a',2:'b',3:'c ...
- 15-----float(浮动)
浮动 浮动是css里面布局最多的一个属性,也是很重要的一个属性. float:表示浮动的意思.它有四个值. none: 表示不浮动,默认 left: 表示左浮动 right:表示右浮动 看一个例子: ...
- android 缓存路径
用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还保 ...
- linux使用echo指令向文件写入内容
echo "aaa">test.sh 该指令会覆盖文件原内容,如果文件不存在,则创建 echo "aaa">>test.sh 追加文件内容 h ...
- Java面向对象_抽象类应用——模板方法模式
概念:定义一个操作中的算法的骨架,而将一些可变部分的实现延迟到子类中.模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤. 去个例子分析一下这个概念: public cla ...
- Hibernate基础案例1
使用到的是MySQL数据库 1.在项目中先引入jar包,并添加引用 <dependencies> <dependency> <groupId>junit</g ...
- Xcode8-beat升级需谨慎
Xcode8-beat版本在打开xib文件的时候,出现了如下的弹窗 在这里要选择Cancel,选择Choose后xib文件的verson会改变,那么Xcode7就没法打开了(坑队友啦), 更没法运行 ...
- ajax 的dataType
这个问题已经碰到过好几次,经常自己挖坑,自己跳,而且还老是犯同样的错.一直纠结 明明其他地方得到的数据格式是对的.为什么这里就是不行.就是自己有洁癖去掉了 dataType:"json&q ...
- Vue.js(2.x)之条件渲染
1.v-if:这里的官网文档看完后赶脚v-if就是用来判断元素是显示还是隐藏. 2.template这个包装元素感觉挺好用,以后把需要某些特定操作才出现的元素存放进去挺好. 3.前面看的网友写的还可以 ...
- 卸载VS2013 2015
我有两个VS,特别讨厌,每当使用window程序删除时候,就出现 停止工作! 然后从知乎上发现了这个 https://github.com/Microsoft/VisualStudioUninstal ...