Dungeon Master (POJ - 2251)(BFS)
转载请注明出处: 作者:Mercury_Lc 地址:https://blog.csdn.net/Mercury_Lc/article/details/82693907
题解:三维的bfs,一开始不怎么理解,就找各种题解,首先要懂的在二维平面上的bfs,bfs一般用来求能够到达某一点使经过的图上的点的值尽可能的小或者是给你两个值x,y,问x能否经过x=2*x或者x+=1这两种操作来变成y(Catch That Cow)。
当然本题是第一种类型,不过除了在平面x0y上下左右行走之外,还可在z轴的方向上行走,这样只需要把那个控制方向的数组开成三维的就可以了,这里注意一点的是,三维数组的第一个是z,其他的地方和二维的类似,就没什么难点了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 60;
int L, R, C;
char gra[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
struct node
{
int x, y, z;
int step;
} S,E,w,l;
int dx[] = {0,0,0,0,1,-1};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {1,-1,0,0,0,0};
void bfs()
{
memset(vis,false,sizeof(vis));
vis[S.z][S.x][S.y] = true;
queue<node>q;
q.push(S);
while(!q.empty())
{
w = q.front();
q.pop();
if(w.x==E.x&&w.y==E.y&&w.z==E.z)
{
printf("Escaped in %d minute(s).\n", w.step);
return ;
}
for(int i = 0; i < 6; i ++)
{
l = w;
l.x += dx[i];
l.y += dy[i];
l.z += dz[i];
if(l.z>=0&&l.z<L&&l.x>=0&&l.x<R&&l.y>=0&&l.y<C&&gra[l.z][l.x][l.y]!='#'&& !vis[l.z][l.x][l.y])
{
l.step++;
q.push(l);
vis[l.z][l.x][l.y] = true;
}
}
}
printf("Trapped!\n");
}
int main()
{
while(~scanf("%d %d %d", &L, &R, &C)&&L &&R && C)
{
getchar();
for(int i =0; i < L; i ++)
{
for(int j =0; j < R; j++)
{
scanf("%s", gra[i][j]);
for(int w = 0; w < C; w ++)
{
if(gra[i][j][w] == 'S')
{
S.z = i;
S.x = j;
S.y = w;
S.step = 0;
}
else if(gra[i][j][w] == 'E')
{
E.z = i;
E.x = j;
E.y = w;
}
}
}
}
bfs();
}
return 0;
}
Problem
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 lineTrapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0Sample Output
Escaped in 11 minute(s).
Trapped!
Dungeon Master (POJ - 2251)(BFS)的更多相关文章
- Dungeon Master(poj 2251)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- HDU 4845 拯救大兵瑞恩(分层图状压BFS)
拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Sub ...
- [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)
快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...
- Hie with the Pie(POJ 3311状压dp)
题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...
- Yogurt factory(POJ 2393 贪心 or DP)
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8205 Accepted: 4197 De ...
- Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)
传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...
- hdu4435 charge-station(先建后拆+bfs)
charge-station Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- UVA 1599 Ideal Path(bfs1+bfs2,双向bfs)
给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...
随机推荐
- Jmeter之设置线程组运行次数/时间
线程组的设置 线程组运行的次数=线程数*循环次数 Ramp-Up Period:表示启动时间 例如:线程数:10,循环次数:10,Ramp-Up Period:2 表示,这个线程组一共有100个线程( ...
- Springboot导出Excel并下载
引入相关依赖 <!--数据导出excel--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> & ...
- Java 多线程创建和线程状态
一.进程和线程 多任务操作系统中,每个运行的任务是操作系统运行的独立程序. 为什么引进进程的概念? 为了使得程序能并发执行,并对并发执行的程序加以描述和控制. 因为通常的程序不能并发执行,为使程序(含 ...
- js 操作对象的小技巧
来源:https://www.w3cplus.com/javascript/javascript-tips.html 1.使用...运算符合并对象或数组中的对象 同样使用ES的...运算符可以替代人工 ...
- dubbo和zookeeper结合使用
1.相关依赖引入 阿里的dubbo包含了spring相关内容,引入依赖时,需要排除,使用我们自己项目中的spring依赖 <!-- start..............dubbo....... ...
- Tomcat项目自动部署脚本
一般情况下使用的Linux环境都是加固的,root路径只有超级管理员权限才能进入.我们新建一个自己的用户,在/home下会有一个用户目录,传输war包都放在这个目录下,此时不动webapps文件下的内 ...
- SQL Server 2005 实现数据库同步备份 过程--结果---分析
数据库复制: 简单来说,数据库复制就是由两台服务器,主服务器和备份服务器,主服务器修改后,备份服务器自动修改. 复制的模式有两种:推送模式和请求模式,推送模式是主服务器修改后,自动发给备份服务器, ...
- vue+ckEditor5
1.安装依赖 "@ckeditor/ckeditor5-build-balloon": "^10.1.0", "@ckeditor/ckeditor5 ...
- How to: Compile Linux kernel 2.6
Compiling custom kernel has its own advantages and disadvantages. However, new Linux user / admin ...
- 自己实现strcat函数
问题:自己实现一个strcat_s函数,要和C语言库函数的strcat函数完成同样的功能. (1) 函数原型 char *strcat(char *dest, const char *src); (2 ...