NYOJ353 3D dungeon 【BFS】
3D dungeon
- 描写叙述
- 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】的更多相关文章
- NYOJ 353 3D dungeon 【bfs】
题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动 ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【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, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
随机推荐
- 边记边学PHP-(十五)MySQL数据库基础操作2
四.使用可视化工具创建数据库 尽管使用命令行感觉更像我们程序猿,可是我还是比較喜欢使用workbench来创建数据库. 首先打开workbench , 一个比較友好的界面就打开了,哈哈.我还是比較喜欢 ...
- iOS相机去黑框
自己定义相机的时候,调用系统的相机,因为相机的分辨率,会出现短小的矩形框,总会出现黑色边框,例如以下图: 假设想实现全屏相机的话,这样做就能够了: CALayer *viewLayer = self. ...
- DiskFileUpload类
1.2.2 DiskFileUpload类 DiskFileUpload类是Apache文件上传组件的核心类,应用程序开发者通过这个类来与Apache文件上传组件进行交互.以下介绍DiskFileUp ...
- Redis c/c++, java client连接
Redis 介绍 redis这个想必大家都了解,关于redis的安装參考这里,redis使用文档參见这里,英文文档. Redis Cclient的用法 Redis的cclient Hiredis使用比 ...
- android判断是否含有某权限
boolean has_permission = (PackageManager.PERMISSION_GRANTED == pkm.checkPermission("android.per ...
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2
这俩个题 题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...
- poj 2935 Basic Wall Maze
是一个图论的基础搜索题- 没什么好说的就是搜索就好 主要是别把 代码写的太屎,错了不好找 #include<cstdio> #include<algorithm> #inclu ...
- shell 脚本运算符
1.数值 格式: test "num1" opr "num2" [ "num1" opr "num2" ] opr 取值 ...
- Android - 用Fragments实现动态UI - 创建灵活的UI
当设计程序来支持各种不一样的屏幕尺寸时,可以在不同的布局中重用fragment来根据可用的屏幕大小来优化用户体验. 例如,在手机上可能使用一个fragment来使用单窗口用户体验比较合适.但是,你可能 ...
- PowerDesigner有几个需要设置
安装后PDM后.一些易于使用的设计人才需求. 一. 建立name与code做自己的主动关联,我们输入name当你不希望个别及连带code已经改变. 打开Tools->General Option ...