【POJ2251】Dungeon Master
本题知识点:宽度优先搜索
题意简单。在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径。每走一格每上或者下一层都算1步。
一开始以为这个“立体迷宫”有点吓到我(题做得太少了),后来发觉,只是一个三维数组以及多了两个操作方向(原地向上或者原地向下),除此之外就是简单的bfs模板题了。
数据很小。
// POJ 2251
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
// R == H, C == W
int L, R, C;
char maze[35][35][35];
int len[35][35][35];
bool stay[35][35][35];
int bl, br, bc, el, er, ec;
int rl[] = { 0, 0, 0, 0, 1, -1 };
int rc[] = { 1, -1, 0, 0, 0, 0 };
int rr[] = { 0, 0, 1, -1, 0, 0 };
struct node{
int l, r, c;
};
queue<node> que;
void build(){
for(int i = 0; i < L; i++){
for(int j = 0; j < R; j++){
scanf("%s", maze[i][j]);
// 找起始点
for(int k = 0; k < C; k++){
if(maze[i][j][k] == 'S'){
bl = i; br = j; bc = k;
}
if(maze[i][j][k] == 'E'){
el = i; er = j; ec = k;
}
}
}
}
// 注意队列一定要清空!
while(!que.empty()) que.pop();
}
void show(){
cout << "len\n";
for(int i = 0; i < L; i++){
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
printf("%d ", len[i][j][k]);
} cout << endl;
} cout << endl;
} cout << endl;
}
void bfs(){
node a;
a.l = bl; a.r = br; a.c = bc;
len[bl][br][bc] = 0;
stay[bl][br][bc] = true;
que.push(a);
while(!que.empty()){
node now = que.front(), next; que.pop();
if(now.l == el && now.r == er && now.c == ec){
break;
}
// 这里比较写的有点花
for(int i = 0; i < 6; i++){
next.l = now.l + rl[i]; next.r = now.r + rr[i]; next.c = now.c + rc[i];
if(0 <= next.l && next.l < L && 0 <= next.r && next.r < R && 0 <= next.c && next.c < C
&& maze[next.l][next.r][next.c] != '#' && !stay[next.l][next.r][next.c]){
stay[next.l][next.r][next.c] = true;
len[next.l][next.r][next.c] = len[now.l][now.r][now.c] + 1;
que.push(next);
}
}
}
}
int main()
{
while(scanf("%d %d %d", &L, &R, &C) && L + R + C != 0){
memset(stay, false, sizeof(stay));
memset(len, -1, sizeof(len));
build();
// bfs
bfs();
// show();
if(len[el][er][ec] != -1)
printf("Escaped in %d minute(s).\n", len[el][er][ec]);
else printf("Trapped!\n");
}
return 0;
}
//3 3 3
//S.#
//###
//###
//
//#.#
//###
//###
//
//#E#
//###
//###
【POJ2251】Dungeon Master的更多相关文章
- 【搜索】Dungeon Master
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+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 【SaltStack】通过Master给Minion安装MySQL
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 【SaltStack】在Master上给Minion端安装zabbix
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- 【leetcode】Dungeon Game (middle)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【转】Spark:Master High Availability(HA)高可用配置的2种实现
原博文出自于: 感谢! Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个 ...
- 【摘录】JDBC Master Slave(JDBC方式的JMS集群)
JDBC Master Slave First supported in ActiveMQ version 4.1 If you are using pure JDBC and not using t ...
随机推荐
- 如何让 height:100%; 起作用---父级元素必须设定高度
参考: http://www.webhek.com/post/css-100-percent-height.html https://www.cnblogs.com/kunmomo/p/1060066 ...
- 05、MySQL—字符集
1.字符编码概念 字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等. 在计算机中所看到的任何内容都是字符构成的. 字符编码(character code)是 ...
- 利用vba实现excel表格连接打印编号(一页两个编号),编号支持前缀
先看一下excel文件, 下图左边部分为文件签审单为要打印的内容, 要求一页需要打印两个文件签审单, NO需要根据打印页面连续编号, 右边部分为打印设置,以及vba部分代码展示, 打印设置可以设置打印 ...
- 在Windows中运行Linux bash命令的几种方法
如果你正在课程中正在学习 shell 脚本,那么需要使用 Linux 命令来练习命令和脚本. 你的学校实验室可能安装了 Linux,但是你自己没有安装了 Linux 的笔记本电脑,而是像其他人一样的 ...
- Commander基本使用
随着NodeJs的不断发展,对于前端来说要做的东西也就更多,Vue脚手架React脚手架等等等一系列的东西都脱颖而出,进入到人们的视野当中,对于这些脚手架工具来讲也只是停留在应用阶段,从来没有想过脚手 ...
- PageRank网页价值算法
一.简介 PageRank是Google提出的算法,用于衡量特定网页相对于其它网页而言的重要程度.是Google创始人拉里.佩奇和谢尔盖.布林于1997年创造的,用于实现将链接价值概念作为排名的重要因 ...
- EF 批量增删改 EntityFramework.Extensions
EntityFramework.Extensions 1.官方网站 http://entityframework-extensions.net/ 2 破解版 Z.EntityFramework.E ...
- HBase集群环境搭建v2.0
本文档环境基于ubuntu16.04版本,如果最终不使用SuperMap iServer 10i ,可以不配置geomesa-hbase_2.11-2.2.0-bin.tar.gz 相比1.0版本,升 ...
- JavaScript笔记01_基本操作
目录 1. JS代码编写的位置 2. 为什么JavaScript中代码要以分号结束 3. 字面量和变量 4. 数据类型 5. 类型装换 6. ++a和a++ 7. 逻辑运算符 8. 相等运算符 9. ...
- secureCRT 中各种传输协议分析 启动daemon运行守护进程(转)
转载链接:http://blog.sina.com.cn/s/blog_61798d5d01018yk4.html [Telnet]著名的终端访问协议,传统的网络服务程序,如FTP.POP和Telne ...