这题是一道简单的广搜题目,读入的时候,需要注意,如果是用scanf读入的话,就直接读取每行的字符串,不然的话,行尾的回车,也会被当成字符读入,这样的话,每次读取的数目就会小于我们想要的数目,因为每次把回车当成迷宫读入了嘛。

所以如果直接读入一个字符串的话,我们就把回车一并读入,但是不用它就可以了。

如果是用cin读入的话,我们就可以用三重循环读入每一个字符,cin会自动跳过空白符。

还有就是cin即使关闭和stdio的同步,也依旧可以和stdio混用。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int maze[35][35][35];
int vis[35][35][35];
int sl, sr, sc, el, er, ec, L, R, C;
int d[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; struct Step {
int l, r, c, step;
}; int check(int i,int j,int k)
{
if (i<0||j<0||k<0||i>=L||j>=R||k>=C)
return 1;
if (maze[i][j][k]==0)
return 1;
if (vis[i][j][k])
return 1;
return 0;
} int bfs()
{
queue<Step> q;
vis[sl][sr][sc] = 1;
Step head,next;
head.l = sl;
head.r = sr;
head.c = sc;
head.step = 0;
q.push(head);
while (!q.empty()) {
Step a = q.front();
q.pop();
if (a.l==el&&a.r==er&&a.c==ec)
return a.step;
for (int i = 0; i < 6;i++) {
next.l = a.l + d[i][0];
next.r = a.r + d[i][1];
next.c = a.c + d[i][2];
if (check(next.l,next.r,next.c))
continue;
vis[next.l][next.r][next.c] = 1;
next.step = a.step + 1;
q.push(next);
}
}
return 0;
} int main()
{
char ch;
ios::sync_with_stdio(false);
while (cin>>L>>R>>C&&L+R+C) {
memset(maze, 0, sizeof(maze));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < L;i++) {
for (int j = 0; j < R;j++) {
for (int k = 0; k < C;k++) {
cin >> ch;
if (ch=='.') {
maze[i][j][k] = 1;
}
else if (ch=='S') {
sl = i, sr = j, sc = k;
maze[i][j][k] = 1;
}
else if (ch=='E') {
el = i, er = j, ec = k;
maze[i][j][k] = 1;
}
}
}
}
int min = bfs();
if (min)
printf("Escaped in %d minute(s).\n", min);
else
printf("Trapped!\n");
// if (min)
// cout << "Escaped in " << min << " minute(s)." << endl;
// else
// cout << "Trapped!" << endl;
}
return 0;
}

POJ-2251-地下城的更多相关文章

  1. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  2. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  3. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  4. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  5. 【POJ 2251】Dungeon Master(bfs)

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

  6. 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 ...

  7. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  8. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  9. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  10. poj 2251

    http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...

随机推荐

  1. apringboot aop权限控制

    + 定义切面: ···@Aspect @Component public class LoginInterceptor { @Around("@annotation(lock)") ...

  2. 记录下java的个人测试方法

    IDEA,用 JUnitGenerator V2.​ 0 做单元测试.. 如果是 SpringBoot,测试类上面加注解 @RunWith(SpringJUnit4ClassRunner.class) ...

  3. (转) Git

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  4. Python-7-字典方法

    clear 删除所有字典项 >>> d = {} >>> d['name'] = 'Gumby' >>> d['age'] = 42 >&g ...

  5. [poj 2104] K-th Number【主席树】

    传送门:http://poj.org/problem?id=2104 保存模版. #include <cstdio> #include <algorithm> #include ...

  6. selenium登录 京东滑动验证码

    京东的滑动验证码在页面上是没有原图的,所有我是用ps把他们拼成一个的. from selenium import webdriver from selenium.webdriver import Ac ...

  7. python学习之j进程和线程:

    每个进程至少有一个线程,python因为每个线程都共用一个GIL全局锁(同时只能运行一个线程),所以不能用多线程(除非重新写C解释器),但是多进程的GIL锁各自独立可多进程. 进程与线程的区别在于一个 ...

  8. Azkaban的功能特点(二)

    Azkaban是什么?(一) 不多说,直接上干货! http://www.cnblogs.com/zlslch/category/938837.html Azkaban的功能特点 它具有如下功能特点: ...

  9. Spark Mllib里数据集如何取前M行(图文详解)

    不多说,直接上干货! 见具体, Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第13章 使用决策树二元分类算法来预测分类StumbleUpon数据集 见具体 Hadoop+Spark大 ...

  10. springmvc整合elasticsearch

    网上大多时关于springboot整合的,也有spring的,但是 坑太多,都没法愉快的玩耍 这篇让我整合成功 https://www.cnblogs.com/sunny1009/articles/7 ...