原题目:
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!
三维求最短路,S起点,E终点,第一个是层数,后两个代表长宽,能不能走到E
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<algorithm>
#include<iomanip>
#define SIS std::ios::sync_with_stdio(false)
#define ll long long
#define INF 0x3f3f3f3f
const int mod = 1e9 + 7;
const double esp = 1e-5;
const double PI = 3.141592653589793238462643383279;
using namespace std;
const int N = 1e6 + 5;
const int maxn = 1 << 20;
/*int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
ll _power(int a, int b)//快速幂
{
int ans=1, res=a;
while(b){
if(b&1) ans=ans*res%mod;
res=res*res%mod;
b>>=1;
}
return ans%mod;*/
char map1[35][35][35];
int vis[35][35][35];
int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
struct node {
int x, y, z;
int step;
}a[N];
int l, r, c;
struct node s;
struct node e;
struct node crup;
bool success(struct node a) {
if (a.x == e.x && a.y == e.y && a.z == e.z)
return true;
else
return false;
}
bool check(int x, int y, int z) {
if ((x >= 0) && (x < l) && (y >= 0) && (y < r) && (z >= 0) && (z < c) && (!vis[x][y][z]) && (map1[x][y][z] == '.' || map1[x][y][z] == 'E'))
return true;
else
return false;
}
void bfs() {
struct node next;
queue<node> q;
q.push(s);//将起始端点放入
while (!q.empty()) {
crup = q.front();
q.pop();
if (success(crup)) {
return;
}
else {
vis[crup.x][crup.y][crup.z] = 1;
for (int i = 0; i < 6; i++) {
next.x = crup.x + base[i][0];
next.y = crup.y + base[i][1];
next.z = crup.z + base[i][2];
if (check(next.x, next.y, next.z)) {
next.step = crup.step + 1;
vis[next.x][next.y][next.z] = 1;
q.push(next);
}
}
}
}
}
int main() {
while (cin >> l >> r >> c) {
if (l == 0 && r == 0 && c == 0)
break;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < l; i++) {
getchar();
for (int j = 0; j < r; j++) {
for (int k = 0; k < c; k++) {
cin >> map1[i][j][k];
if (map1[i][j][k] == 'S') {
s.x = i;
s.y = j;
s.z = k;
s.step = 0;
}
else if (map1[i][j][k] == 'E') {
e.x = i;
e.y = j;
e.z = k;
e.step = 0;
}
}
getchar();
}
}
bfs();
if (crup.x == e.x && crup.y == e.y && crup.z == e.z)
printf("Escaped in %d minute(s).\n", crup.step);
else
printf("Trapped!\n");
}
return 0;
}