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?
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.
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
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!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
又是一道无比蛋疼的题。。调试了N久。开始用深搜,能出结果,但是超时,后来改用广搜,三维数组输入对应这个地方出了点问题。调了好久才过。一直没看到题目上那句红色的,导致用了单个字符输入然后没有给测试数据的回车留位置。。。
#include
#include
using namespace std;
int vis[35][35][35],dirx[6] = {1, -1, 0, 0, 0, 0}, diry[6] = {0, 0, 1, -1, 0, 0}, dirz[6] = {0, 0, 0, 0, 1, -1}, x, y, z, a, b, c, flag, ti;
char m[35][35][35];
typedef struct node
{
int x, y, z, t;
}node;
int bfs(int x, int y, int z)
{
int i;
queue q;
node u;
u.x = x;
u.y = y;
u.z = z;
u.t = 0;
vis[x][y][z] = 1;
q.push(u);
while(!q.empty())
{
u = q.front();
q.pop();
if(m[u.x][u.y][u.z] == 'E')
{
flag = 1;
return u.t;
}
for(i = 0 ; i < 6 ; i++)
{
node v;
v.x = u.x + dirx[i];
v.y = u.y + diry[i];
v.z = u.z + dirz[i];
// if(v.x < 0 || v.x >= a || v.y < 0 || v.y >= b || v.z < 0 || v.z >= c || vis[v.x][v.y][v.z] || m[v.x][v.y][v.z] == '#')
// continue;
if((m[v.x][v.y][v.z] == '.' || m[v.x][v.y][v.z] == 'E') && !vis[v.x][v.y][v.z])
{
vis[v.x][v.y][v.z] = 1;
v.t = u.t + 1;
q.push(v);
}
}
}
return 0;
}
int main()
{
int i, j, k;
while(scanf("%d %d %d", &a, &b, &c) , (a != 0 || b != 0 || c!= 0))
{
ti = 0;
flag = 0;
memset(vis, 0, sizeof(vis));
memset(m, '#', sizeof(m));
for(i = 0 ; i < a ; i++)
for(j = 0 ; j < b ; j++)
{
scanf("%s", m[i][j]);
for(k = 0 ; k < c ; k++)
{
if(m[i][j][k] == 'S')
{
x = i;
y = j;
z = k;
}
}
}
ti = bfs(x, y, z);
if(flag)
printf("Escaped in %d minute(s).\n", ti);
else
printf("Trapped!\n");
}
return 0;
}
红色部分是三维数组的输入方式,这样直接内层循环就是一张,外重循环就是好几张摞起来。
二维数组也可以用这种输入:
a[M][N]
for(i = 0 ; i < m ; i++)
scanf("%s", a[i]);