博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2251 Dungeon Master
阅读量:5290 次
发布时间:2019-06-14

本文共 3602 字,大约阅读时间需要 12 分钟。

Dungeon Master

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on 
PKU. Original ID: 
64-bit integer IO format: %lld      Java class name: Main
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

 
解题:搜索。。。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define LL long long13 #define INF 0x3f3f3f3f14 #define pii pair
15 16 using namespace std;17 const int maxn = 40;18 char table[maxn][maxn][maxn];19 int L,R,C,sx,sy,sz,ex,ey,ez;20 bool vis[maxn][maxn][maxn];21 struct node {22 int x,y,z,step;23 node(int tx = 0,int ty = 0,int tz = 0,int tp = 0) {24 x = tx;25 y = ty;26 z = tz;27 step = tp;28 }29 };30 const int dir[6][3] = {31 { 0,0,-1},//left32 { 0,0,1},//right33 { 0,-1,0},//up34 { 0,1,0},//down35 {-1,0,0},//front36 { 1,0,0}//back37 };38 bool isIn(const node &tmp) {39 int a = (tmp.x >= 0 && tmp.x < L);40 int b = (tmp.y >= 0 && tmp.y < R);41 int c = (tmp.z >= 0 && tmp.z < C);42 return a + b + c == 3;43 }44 queue
q;45 int bfs() {46 while(!q.empty()) q.pop();47 q.push(node(sx,sy,sz,0));48 vis[sx][sy][sz] = true;49 while(!q.empty()) {50 node now = q.front();51 q.pop();52 if(table[now.x][now.y][now.z] == 'E') return now.step;53 for(int i = 0; i < 6; ++i) {54 node tmp(now.x+dir[i][0],now.y+dir[i][1],now.z+dir[i][2],now.step+1);55 if(isIn(tmp)&&table[tmp.x][tmp.y][tmp.z] != '#' &&!vis[tmp.x][tmp.y][tmp.z]) {56 vis[tmp.x][tmp.y][tmp.z] = true;57 if(table[tmp.x][tmp.y][tmp.z] == 'E') return tmp.step;58 q.push(tmp);59 }60 }61 }62 return -1;63 }64 int main() {65 while(scanf("%d %d %d",&L,&R,&C),L||R||C) {66 memset(table,'\0',sizeof(table));67 memset(vis,false,sizeof(vis));68 for(int i = 0; i < L; ++i) {69 for(int j = 0; j < R; ++j) {70 scanf("%s",table[i][j]);71 for(int k = 0; k < C; ++k)72 if(table[i][j][k] == 'S') {73 sx = i;74 sy = j;75 sz = k;76 }77 }78 }79 int ans = bfs();80 if(~ans) printf("Escaped in %d minute(s).\n",ans);81 else puts("Trapped!");82 }83 return 0;84 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4236289.html

你可能感兴趣的文章
mysql中key 、primary key 、unique key 与index区别
查看>>
bzoj2257
查看>>
Linux查看文件编码格式及文件编码转换<转>
查看>>
Leetcode: Find Leaves of Binary Tree
查看>>
Vue 模板解释
查看>>
http://www.bootcss.com/
查看>>
20145308 《网络对抗》 注入shellcode+Return-to-libc攻击 学习总结
查看>>
将多张图片和文字合成一张图片
查看>>
自己动手写ORM(01):解析表达式树生成Sql碎片
查看>>
如何使用USBWebserver在本机快速建立网站测试环境
查看>>
百度Ueditor编辑器的Html模式自动替换样式的解决方法
查看>>
变量提升
查看>>
线性表可用顺序表或链表存储的优缺点
查看>>
在现有的mysql主从基础上,搭建mycat实现数据的读写分离
查看>>
opencv安装配置
查看>>
JAVA-初步认识-第六章-面向对象(举例)
查看>>
js合并数组
查看>>
cNoteSetCursor_命令窗口光标设置
查看>>
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>