题意如下
描述Is an escape possible? If yes, how long will it take?
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.
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!
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<memory.h>
#include<queue>
#include<string>
using namespace std;
int l, r, c, sx, sy,sz,ex,ey,ez;
char map1[32][32][32];
int bo[32][32][32];
char ch;
int dz[6] = { 0,0,0,0,1,-1 };
int dx[6] = { 0,0,1,-1,0,0 };
int dy[6] = { 1,-1,0,0,0,0 };
struct node
{
int x, y, z, step;
node(int a, int b, int c, int d)
{
x = a; y = b; z = c; step = d;
}
};
bool check(int xx, int yy, int zz)
{
if ((xx < 0) || (xx >= r) || (yy < 0) || (yy >= c) || (zz < 0) || (zz >= l)) return false;
if (bo[zz][xx][yy] == false) return false;
if (map1[zz][xx][yy] == '#') return false;
return true;
}
void bfs1()
{
memset(bo, true, sizeof(bo));
queue<node> q;
int x1, y1, z1;
while (!q.empty()) q.pop();
q.push(node(sx, sy, sz, 0));
bo[sz][sx][sy] = false;
bool flag = true;
while (!q.empty())
{
node a = q.front();
q.pop();
if ((a.x == ex) && (a.y == ey) && (a.z == ez))
{
printf("Escaped in %d minute(s).\n", a.step);
flag = false;
break;
}
for (int i = 0; i < 6; i++)
{
x1 = a.x + dx[i];
y1 = a.y + dy[i];
z1 = a.z + dz[i];
if (!check(x1, y1, z1)) continue;
// cout << "i will be here" << endl;
q.push(node(x1, y1, z1, a.step+1));
bo[z1][x1][y1] = false;
}
}
if (flag == true) printf("Trapped!\n");
}
int main()
{
while (1)
{
scanf("%d %d %d", &l, &r, &c);
if ((l + r + c) == 0) break;
for (int ll = 0; ll < l; ll++)
{
for (int rr = 0; rr < r; rr++)
{
scanf("%s", map1[ll][rr]);
for (int cc = 0; cc < c; cc++)
{
if (map1[ll][rr][cc] == 'S')
{
sx = rr; sy = cc; sz = ll;
continue;
}
if (map1[ll][rr][cc] == 'E')
{
ex = rr; ey = cc; ez = ll;
continue;
}
}
}
}
//for (int ll = 0; ll < l; ll++)
// for (int rr = 0; rr < r; rr++)
// printf("%s", map1[ll][rr]);
//printf("sx:%d sy:%d sz:%d\n", sx, sy, sz);
//printf("ex:%d ey:%d ez:%d\n", ex, ey, ez);
bfs1();
}
return 0;
}