题目链接

题目大意:

三维迷宫,搜索从s到e的最小步骤数。

分析:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
#include <queue> using namespace std; const int maxn = ; int dx[] = {, , -, , , };
int dy[] = {, , , , -, };
int dz[] = {-, , , , , }; char G[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int r, l, c; struct Pos {
int x, y, z;
int step;
}; int BFS(int x0, int y0, int z0) {
queue<Pos> Q; Q.push((Pos){x0, y0, z0, }); while(!Q.empty()) {
Pos e = Q.front(); Q.pop();
int x=e.x, y=e.y, z=e.z, s=e.step; if(G[z][x][y] == 'E') return s; for(int d=; d<; d++) {
int nx, ny, nz;
nx = x+dx[d];
ny = y+dy[d];
nz = z+dz[d]; if(nx < || ny < || nz < ) continue;
if(nx >= r || ny >= c || nz >= l) continue;
if(vis[nz][nx][ny]) continue;
if(G[nz][nx][ny] == '#') continue; vis[nz][nx][ny] = true; Q.push((Pos){nx, ny, nz, s+});
}
} return -;
} int main() {
int x0, y0, z0; while(scanf("%d%d%d", &l, &r, &c) == ) {
if(l == && r == && c == ) break; for(int i=; i<l; i++) {
for(int j=; j<r; j++) {
scanf("%s", G[i][j]);
}
} for(int i=; i<l; i++) {
for(int j=; j<r; j++) {
for(int k=; k<c; k++) {
if(G[i][j][k] == 'S'){
z0 = i; x0=j; y0=k;
}
}
}
} memset(vis, false, sizeof(vis)); int res = BFS(x0, y0, z0); if(res == -) {
printf("Trapped!\n");
}
else
printf("Escaped in %d minute(s).\n", res);
} return ;
}

POJ2251 Dungeon Master(bfs)的更多相关文章

  1. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  4. POJ-2251 Dungeon Master (BFS模板题)

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

  5. POJ2251——Dungeon Master(三维BFS)

    和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向. 用上队列的进出操作较为轻松. #include<iostream> #include& ...

  6. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  7. poj 2251 Dungeon Master (BFS 三维)

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

  8. [poj] Dungeon Master bfs

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

  9. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

随机推荐

  1. java编译期优化与执行期优化技术浅析

    java语言的"编译期"是一段不确定的过程.由于它可能指的是前端编译器把java文件转变成class字节码文件的过程,也可能指的是虚拟机后端执行期间编译器(JIT)把字节码转变成机 ...

  2. 计算任意位数的Pi

    当用程序实现求pi的值时,也许你能够很快写出算法(利用求pi的几个公式),但是由于使用单变量保存结果,限于计算机硬件对变量的表示范围有限,因此,最多只能计算出pi值小数点后十多位.但需要得到一个更大位 ...

  3. Swift: 继承

    为了在属性值改变的时候获得通知,类可以为继承的属性添加属性观察者.属性观察者可以添加到任何属性上,不管这个属性原来是存储属性还是计算属性. Swift中的类没有一个统一的基类. 为了讲明白继承,我们先 ...

  4. 使用ajax与服务器通信的步骤

    使用ajax与服务器通信的步骤: 1. 创建一个XMLHttpRequest对象 2. 创建url,data,通过xmlHttpRequest.send() 3. 服务器端接收ajxa的请求,做相应处 ...

  5. 学习JAVA第一部分总结

    把自己这几天的学习情况记录下来. 第一章,认识JAVA,了解JAVA的运行机制,虚拟机. 第二章,了解java的注释,标识符,关键字.. 第三章,基本的数据类型,byte short int long ...

  6. iOS程序的完整启动过程(有storyboard)

    1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情:1> 创建UIApplication对象 2> ...

  7. 高级I/O函数(3)-tee、fcntl函数

    tee函数使用 功能描述:tee函数在两个管道文件描述符之间复制数据,也是零拷贝操作.它不消耗数据,因此源文件描述符仍然可以用于后续的操作. 函数原型: #include <fcntl.h> ...

  8. 给出2n+1个数,其中有2n个数出现过两次,如何用最简便的方法找出里面只出现了一次的那个数(转载)

    有2n+1个数,其中有2n个数出现过两次,找出其中只出现一次的数 例如这样一组数3,3,1,2,4,2,5,5,4,其中只有1出现了1次,其他都是出现了2次,如何找出其中的1? 最简便的方法是使用异或 ...

  9. Ajax.BeginForm 防止跳转到新页面

    @using (Ajax.BeginForm("ChangeCompanyId", "navigation", new { area = "confi ...

  10. copy(source,destination)拷贝文件

    source 必须 规定要复制的文件 destination 复制文件的目的地 说明 :将文件从 source 拷贝到 destination.如果成功则返回 TRUE,否则返回 FALSE. 例如: ...