POJ 2251 Dungeon Master bfs
2016-04-11 20:41
537 查看
Dungeon Master
Description
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?
Input
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.
Output
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!
Sample Input
Sample Output
Source
Ulm Local 1997
3d bfs//原来不知道哪里错了盯着代码debug半天没结果 重新手敲一遍就a了- -
ACcode:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define maxn 33
using namespace std;
char mapp[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int to[][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
int n,m,l;
struct N{
int x,y,z,t;
};
N my,no,ne;
bool ju(N a){
if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=l||mapp[a.x][a.y][a.z]=='#'||vis[a.x][a.y][a.z])return 0;
return 1;
}
void bfs(){
memset(vis,false,sizeof(vis));
queue<N>q;
q.push(my);
vis[my.x][my.y][my.z]=1;
while(!q.empty()){
no=q.front();q.pop();
for(int i=0;i<6;++i){
ne=no;
ne.x+=to[i][0];
ne.y+=to[i][1];
ne.z+=to[i][2];
ne.t++;
if(ju(ne)){
if(mapp[ne.x][ne.y][ne.z]=='E'){
printf("Escaped in %d minute(s).\n",ne.t);
return;
}
q.push(ne);
vis[ne.x][ne.y][ne.z]=1;
}
}
}
printf("Trapped!\n");
}
int main(){
while(~scanf("%d%d%d",&n,&m,&l)&&(n+m+l)){
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
for(int z=0;z<l;++z){
cin>>mapp[i][j][z];
if(mapp[i][j][z]=='S'){
my.x=i;
my.y=j;
my.z=z;
my.t=0;
}
}
bfs();
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24602 | Accepted: 9525 |
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?
Input
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.
Output
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!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
Ulm Local 1997
3d bfs//原来不知道哪里错了盯着代码debug半天没结果 重新手敲一遍就a了- -
ACcode:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define maxn 33
using namespace std;
char mapp[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int to[][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
int n,m,l;
struct N{
int x,y,z,t;
};
N my,no,ne;
bool ju(N a){
if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=l||mapp[a.x][a.y][a.z]=='#'||vis[a.x][a.y][a.z])return 0;
return 1;
}
void bfs(){
memset(vis,false,sizeof(vis));
queue<N>q;
q.push(my);
vis[my.x][my.y][my.z]=1;
while(!q.empty()){
no=q.front();q.pop();
for(int i=0;i<6;++i){
ne=no;
ne.x+=to[i][0];
ne.y+=to[i][1];
ne.z+=to[i][2];
ne.t++;
if(ju(ne)){
if(mapp[ne.x][ne.y][ne.z]=='E'){
printf("Escaped in %d minute(s).\n",ne.t);
return;
}
q.push(ne);
vis[ne.x][ne.y][ne.z]=1;
}
}
}
printf("Trapped!\n");
}
int main(){
while(~scanf("%d%d%d",&n,&m,&l)&&(n+m+l)){
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
for(int z=0;z<l;++z){
cin>>mapp[i][j][z];
if(mapp[i][j][z]=='S'){
my.x=i;
my.y=j;
my.z=z;
my.t=0;
}
}
bfs();
}
}
相关文章推荐
- FFmpeg学习笔记004_视音频编解码技术零基础由浅入深
- BZOJ3207: 花神的嘲讽计划Ⅰ
- 《机器学习基石》作业一第17题的程序
- Centos6.5 ssh修改默认端口号
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- 策略模式
- 山东省第一届ACM省赛 B SDUT 2152 Balloons(简单的BFS)
- Wireshark:图形化数据包捕获软件。
- 栈求解迷宫问题
- Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择
- c++ 链接mysql:error LNK2019: 无法解析的外部符号
- 完全卸载MySQL数据库
- CCF之消除类游戏
- jsp页面中的$(param.xxxxx)
- 近 100 个 Linux 常用命令大全
- “耐撕”团队 2016.04.11 站立会议
- application/x-www-form-urlencoded multipart/form-data text/plain 后台返回的数据响应的格式类型
- linux 安装jdk
- Matlab协助调试ROS程序: matlab2015b + ros indigo
- Convolutional Neural Networks卷积神经网络 (该博主一系列都很好)