3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描写叙述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not
be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 



Is an escape possible? If yes, how long will it take?

输入
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 

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.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 

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!
例子输入
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
例子输出
Escaped in 11 minute(s).
Trapped!

#include <stdio.h>
#include <string.h>
#include <queue>
using std::queue;
char map[32][32][32], vis[32][32][32];
int a, b, c, X, Y, Z;
const int mov[][3] = {0, 0, 1, 0, 0, -1, 0, 1,
0, 0, -1, 0, 1, 0, 0, -1, 0, 0};
struct Node{
int x, y, z, steps;
};
queue<Node> Q; bool check(Node t){
if(t.x < 0 || t.y < 0 || t.z < 0) return 0;
if(t.x >= a || t.y >= b || t.z >= c) return 0;
if(vis[t.x][t.y][t.z] || map[t.x][t.y][t.z] == '#') return 0;
return 1;
} void BFS(){
while(!Q.empty()) Q.pop(); Node t, n = {0};
n.x = X; n.y = Y; n.z = Z;
vis[X][Y][Z] = 1;
Q.push(n); while(!Q.empty()){
n = Q.front(); Q.pop(); for(int i = 0; i < 6; ++i){
t = n; ++t.steps;
t.x += mov[i][0];
t.y += mov[i][1];
t.z += mov[i][2]; if(check(t)){
if(map[t.x][t.y][t.z] == 'E'){
printf("Escaped in %d minute(s).\n", t.steps);
return;
}
vis[t.x][t.y][t.z] = 1;
Q.push(t);
}
}
}
printf("Trapped!\n");
} int main(){
while(scanf("%d%d%d", &a, &b, &c), a || b || c){ memset(vis, 0, sizeof(vis)); for(int i = 0; i < a; ++i){
for(int j = 0; j < b; ++j){
scanf("%s", map[i][j]);
for(int k = 0; k < c; ++k)
if(map[i][j][k] == 'S'){
X = i; Y = j; Z = k;
}
}
}
BFS();
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

NYOJ353 3D dungeon 【BFS】的更多相关文章

  1. NYOJ 353 3D dungeon 【bfs】

    题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动 ...

  2. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  3. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  4. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  5. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  6. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  7. 【bfs】迷宫问题

    [题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...

  8. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  9. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

随机推荐

  1. The app references non-public selectors in payload With Xcode6.1

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: p=591" style="color: rgb(255, 97, 0 ...

  2. 4pdf

    http://www.cnblogs.com/haocool/archive/2013/03/16/2962547.html

  3. 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider

    原文:重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider [源码下载] 重新想象 Wind ...

  4. APUE学习总结

    简介 本文总结了个人,一个数字,对应称号<APUE>第一版的每一章,但是,独立的二级标题和书,人需求进行编写. 3.文件I/O 本章所说明的函数常常被称之为不带缓存的I/O(与第5章中说明 ...

  5. 左右xcode的重构选项的一些理解

    Rename(重命名):对标示符进行重命名,以获得更好的代码可读性,这些标示符包含类,方法或者函数的名称. Extract(抽取):将你在XCode种选择的代码抽取到一个新的方法或函数中. Creat ...

  6. uml 9图不同的角度品种分类

    只要阅读uml视频后为其9一些模糊样图或理解.话又说回来后来他们自己的系统看着笔记,统的了解.以下分别从两个不同的角度对uml中9种图进行理解以及当中某些图的区分,本人比較菜,有些不完好的地方欢迎提出 ...

  7. solr与.net课程(七)solr主从复制

    既然solr是解决大量数据全文索引的方案,因为高并发的问题,我们就要考虑solr的负载均衡了,solr提供很easy的主从复制的配置方法,那么以下我们就来配置一下solr的主从复制 如果我们在192. ...

  8. Axis2 -POJO

    POJO,Plain Old Java Object,简单Java物. 通告Webservice 1.书写Hello.java public class Hello { public String s ...

  9. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  10. 【剑指offer学习】求和为定值的两个数(拓展)

    接着上面一篇文章: http://blog.csdn.net/u013476464/article/details/40651451 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...