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. 说说UI设计

    近期的项目验收中,无数次的提到了UI的设计,首先来说说为什么UI设计如此重要. 对于用户来说产品的外观是最先映入眼帘的,无论你用了什么高端的技术,无论你后台代码封装的多么好,用户是无法体会到的,能体会 ...

  2. repo总结

    repo刚google使用Python脚本写通话git脚本.主要用于下载.管理Android工程仓库. 1. 下载 repo 的地址: http://android.git.kernel.org/re ...

  3. Android自己定义组件系列【5】——高级实践(1)

    在接下来的几篇文章将任老师的博文<您可以下拉PinnedHeaderExpandableListView实现>骤来具体实现.来学习一下大神的代码并记录一下. 原文出处:http://blo ...

  4. 手动安装英特尔® 凌动™ Android* x86 模拟器映像

    android的模拟器实在是太慢了,慢的让人欲仙欲死,欲罢不能.猛然发现我的电脑是intel的CPU,我勒个去,换x86模拟器.然后悲剧了,伟大的gfw 我要装sdk,我要研究android开发,到底 ...

  5. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  6. String.format()【演示具体的例子来说明】

    String.format()[演示样例具体解释] 整理者:Vashon 前言: String.format 作为文本处理工具.为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 Strin ...

  7. SRM 620 D2L3: RandomGraph, dp

    称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...

  8. Android-管理Activity生命周期 -暂停和恢复一个Activity

    在正常的使用app时,前台的activity有时候会被可见的组件阻塞导致activity暂停.比如,当打开一个半透明的activity(就像打开了一个对话框),之前的activity就会暂停.只要ac ...

  9. CSS 之 光进入光

    一个.概念 css,层叠样式表(英语:Cascading Style Sheets.简写CSS).又称串样式列表.层次结构式样式表文件,一 种用来为结构化文档(如HTML文档或XML应用)加入样式(字 ...

  10. [Ext JS 4] 实战Chart 协调控制(单一的坐标,两个坐标)

    前言