本题传送门

本题知识点:宽度优先搜索

题意简单。在一个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的更多相关文章

  1. 【搜索】Dungeon Master

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  2. 【POJ - 2251】Dungeon Master (bfs+优先队列)

    Dungeon Master  Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...

  3. 【SaltStack】通过Master给Minion安装MySQL

    一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...

  4. 【SaltStack】在Master上给Minion端安装zabbix

    一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...

  5. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  6. 【leetcode】Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

  7. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  8. 【转】Spark:Master High Availability(HA)高可用配置的2种实现

    原博文出自于: 感谢! Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个 ...

  9. 【摘录】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 ...

随机推荐

  1. Kafka 生产者、消费者与分区的关系

    背景 最近和海康整数据对接, 需要将海康产生的结构化数据拿过来做二次识别. 基本的流程: 海康大数据 --> kafka server --> 平台 Kafka 的 topic 正常过车 ...

  2. Github下载慢和下载过程中断等情况的解决方案

    Github下载慢和下载过程中断等情况的解决方案   最近老大push项目,正常的git clone每次都是下载一部分就断掉了. 尝试了修改hosts文件的方式,更换了延迟最低的域名也没啥用(难道我姿 ...

  3. js 数据类型检测

    提起数据类型检测  大多数人首先想起的应该是  typeof 'xxx' == '数据类型' 坦然这种方法对于基本数据类型的检测还是非常方便的,但是当遇到引用数据类型 Object时却爱莫能助,下面就 ...

  4. js函数(续)

    一.全局变量和局部变量全局变量:当前js页面中均可使用的变量[声明在函数外面的变量],整个js页面中均可以使用.局部变量:声明在函数内部的变量,只能在函数内部使用.eg: var a = 1; con ...

  5. linux ftp虚拟用户的创建

    学习目标: 匿名用户的登录,添加用户的登录,虚拟用户的创建. 虚拟用户的创建: 1.安装:yum -y install vsftpd    服务端 yum -y install ftp         ...

  6. PHP实现微信退款功能

    最近在调微信退款接口,发现有许多坑,更大家分享一下 ① 要是在测试的时候,网页提示 curl 58 说明 证书的路径出现问题(这里要填物理路径,也就是绝对路径) ②网页提示curl 52 说明你的证书 ...

  7. MySQL- Host 'xxx' is not allowed to connect to this MySQL server.

    mysql中有个系统数据库mysql,里面有张表user记录该实例的用户及其权限. 第一种方法 将其用户root的host改为%,即允许所有客户端连接. FLUSH PRIVILEGES;是将修改生效 ...

  8. SolarWinds-改变端口

    Solarwinds配置文件,修改为80端口(默认为8123) C:\Program Files\SolarWinds\DPA\iwc\tomcat\conf\server.xml

  9. SQL SERVER- waitresource解读

    例如: OBJECT: 18:1769220894:8 第一个是dbid:18,第二个是objectid:1769220894,第三个是indexID:8 SELECT DB_NAME(18)SELE ...

  10. linux文件常用操作

    建立目录:mkdir mkdir -p [目录名] -p 递归创建 命令英文原意: make directories 切换所在目录:cd cd [目录] cd ~    进入当前用户的家目录 cd c ...