POJ
来源:互联网 发布:网络剪刀手 win7 编辑:程序博客网 时间:2024/06/05 11:23
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Escaped in 11 minute(s).Trapped!
三维迷宫,bfs搜索一下输出时间就行了~~~
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>using namespace std;typedef struct mg{ int x, y, z, step;}node;char s[99][99][99];int L, R, C, v[99][99][99];int bfs(int x, int y, int z){ node a, b; int t = -1; queue<node>Q; a.x = x; a.y = y; a.z = z; a.step = 0; Q.push(a); v[x][y][z] = 1; while(!Q.empty()) { a = Q.front(); Q.pop(); if(a.z + 1 < C && s[a.x][a.y][a.z + 1] == 'E') { t = a.step + 1; break; } else if(s[a.x][a.y][a.z + 1] != '#' && v[a.x][a.y][a.z + 1] == 0 && a.z + 1 < C) { b.x = a.x; b.y = a.y; b.z = a.z + 1; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } if(a.z - 1 >= 0 && s[a.x][a.y][a.z - 1] == 'E') { t = a.step + 1; break; } else if(s[a.x][a.y][a.z - 1] != '#' && v[a.x][a.y][a.z - 1] == 0 && a.z - 1 >= 0) { b.x = a.x; b.y = a.y; b.z = a.z - 1; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } if(a.y + 1 < R && s[a.x][a.y + 1][a.z] == 'E') { t = a.step + 1; break; } else if(s[a.x][a.y + 1][a.z] != '#' && v[a.x][a.y + 1][a.z] == 0 && a.y + 1 < R) { b.x = a.x; b.y = a.y + 1; b.z = a.z; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } if(a.y - 1 >= 0 && s[a.x][a.y - 1][a.z] == 'E') { t = a.step + 1; break; } else if(s[a.x][a.y - 1][a.z] != '#' && v[a.x][a.y - 1][a.z] == 0 && a.y - 1 >= 0) { b.x = a.x; b.y = a.y - 1; b.z = a.z; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } if(a.x + 1 < L && s[a.x + 1][a.y][a.z] == 'E') { t = a.step + 1; break; } else if(s[a.x + 1][a.y][a.z] != '#' && v[a.x + 1][a.y][a.z] == 0 && a.x + 1 < L) { b.x = a.x + 1; b.y = a.y; b.z = a.z; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } if(a.x - 1 >= 0 && s[a.x - 1][a.y][a.z] == 'E') { t = a.step + 1; break; } else if(s[a.x - 1][a.y][a.z] != '#' && v[a.x - 1][a.y][a.z] == 0 &&a.x - 1 >= 0) { b.x = a.x - 1; b.y = a.y; b.z = a.z; b.step = a.step + 1; v[b.x][b.y][b.z] = 1; Q.push(b); } } return t;}int main(){ int i, k, j, x, z, y, t; while(cin>>L>>R>>C && (L || R || C)) { getchar(); memset(v,0,sizeof(v)); for(i = 0;i < L;i++) { for(j = 0;j < R;j++) { for(k = 0;k < C;k++) { cin>>s[i][j][k]; if(s[i][j][k] == 'S') { x = i; y = j; z = k; } } getchar(); } getchar(); } t = bfs(x,y,z); if(t < 0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",t); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- EL表达式
- winpe下开启vs远调器
- 剑指offer:第29题寻找数组中出现次数超过一半的数字
- 混合开发,原生+html5
- 异常—Cause: java.sql.SQLException: Data source is closed
- POJ
- 【算法】程序猿不写代码是不对的67
- OpenJ_Bailian
- 2017年6月7日头条争夺战:微信公众号居然公然和高考抢头条?!
- Ubuntu14.04下OpenCV安装
- #QGIS源码官方编译指南
- 解决Caffe训练过程中loss不变问题
- 注释驱动的 Spring cache 缓存介绍
- coding之后端经验