Dungeon Master(三维bfs)
题目链接:http://poj.org/problem?id=2251
题目:
Description
Is an escape possible? If yes, how long will it take?
Input
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
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 line
Trapped!
Sample Input
- 3 4 5
- S....
- .###.
- .##..
- ###.#
- #####
- #####
- ##.##
- ##...
- #####
- #####
- #.###
- ####E
- 1 3 3
- S##
- #E#
- ###
- 0 0 0
Sample Output
- Escaped in 11 minute(s).
- Trapped!
题意:你处在一个三维的地牢里,从S出发逃到出口E,问最少要跑多远。- 思路:这题虽然是一个三维的地图,但是做法和二维的没多大区别,不过要从当前层到其他层的要求是你所在位置为非#,且你将到的那层的这个位置也是非#。
- 代码实现如下:
- #include <queue>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int inf = 0x3f3f3f3f;
- int l, r, c, ans;
- int sx, sy, sz;
- char mp[][][];
- int vis[][][];
- struct node {
- int x, y, z, step;
- }nw, nxt;
- int dx[] = {, -, , , , }, dy[] = {, , , -, , },
- dz[] = {, , , , , -};
- void bfs(int z, int x, int y) {
- vis[z][x][y] = ;
- nw.z = z, nw.x = x, nw.y = y, nw.step = ;
- queue<node> q;
- q.push(nw);
- while(!q.empty()) {
- nw = q.front(), q.pop();
- if(mp[nw.z][nw.x][nw.y] == 'E') {
- ans = nw.step;
- return;
- }
- for(int i = ; i < ; i++) {
- nxt.z = nw.z + dz[i];
- nxt.x = nw.x + dx[i];
- nxt.y = nw.y + dy[i];
- if(nxt.z >= && nxt.z < l && nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == && mp[nxt.z][nxt.x][nxt.y] != '#') {
- nxt.step = nw.step + ;
- vis[nxt.z][nxt.x][nxt.y] = ;
- q.push(nxt);
- }
- }
- }
- }
- int main() {
- while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) {
- for(int i = ; i < l; i++) {
- for(int j = ; j < r; j++) {
- scanf("%s", mp[i][j]);
- for(int k = ; k < c; k++) {
- if(mp[i][j][k] == 'S') {
- sx = j, sy = k, sz =i;
- }
- }
- }
- }
- memset(vis, , sizeof(vis));
- ans = inf;
- bfs(sz, sx, sy);
- if(ans >= inf) {
- printf("Trapped!\n");
- } else {
- printf("Escaped in %d minute(s).\n", ans);
- }
- }
- return ;
- }
Dungeon Master(三维bfs)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
随机推荐
- eclipse 创建并运行maven web项目
这两天想在eclipse上运行maven web项目,折腾了许久,总算success啦. 1,利用eclipse创建dynamic web project(eclipse需要安装m2eclipse). ...
- Pandas速查手册中文版(转)
关键缩写和包导入 在这个速查手册中,我们使用如下缩写: df:任意的Pandas DataFrame对象 同时我们需要做如下的引入: import pandas as pd 导入数据 pd.read_ ...
- BIO、NIO、AIO通信机制
一.BIO的理解 首先我们通过通信模型图来熟悉下BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立的Acceptor线程负责监听客户端的连接,它接收到客户端的连接请求之后为每个客户端创 ...
- 第71天:jQuery基本选择器(二)
jQuery选择器 一.内容过滤选择器 选择器 描 述 返 回 示 例 :contains(text) 匹配含有文本内容text的元素 集合元素 $(“p:contains(今天)”) :empty ...
- 第27天:js-表单获取焦点和数组声明遍历
一.表单 1.this指事件的调用者2.input.value 表单更换内容3.innerHTML更换盒子里的内容,文字.标签都能换.4.isNaN("12")如果里面的不是个数字 ...
- 关于 [lambda x: x*i for i in range(4)] 理解
题目: lst = [lambda x: x*i for i in range(4)] res = [m(2) for m in lst] print res 实际输出:[6, 6, 6, 6] 想要 ...
- hadoop的第一个hello world程序(wordcount)
在hadoop生态中,wordcount是hadoop世界的第一个hello world程序. wordcount程序是用于对文本中出现的词计数,从而得到词频,本例中的词以空格分隔. 关于mapper ...
- BZOJ1179 [Apio2009]Atm 【tarjan缩点】
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MB Submit: 4048 Solved: 1762 [Submit][Sta ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- python基础之魔法方法
由于hexo自带的markdown渲染引擎对双下划线做了转义,在正文中看到的魔法方法前后都没有双下划线 setattr.getattr.delattr 可以拦截对对象属性的访问 setattr函数是用 ...