B - Dungeon Master
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?
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
刚开始这道题毫无思绪,后来发现用三维bfs,就当积累经验了
仔细想一想,无非是多加了两个方向,用bfs走一遍其实和二维相差无几
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
char mapp[35][35][35];
struct node
{
int x, y, z;
int step;
};
struct node a;
void init()
{
for(int i = 0; i < 35; i ++)
for(int j = 0; j < 35; j ++)
for(int k = 0; k < 35; k ++)
mapp[i][j][k] = '#';
}
//void input(int x, int y, int z)
//{
// for(int i = 1; i <= z; i++)
// {
// getchar();
// for(int j = 1; j <= x; j ++)
// {
// for(int k = 1; k <= y; k ++)
// {
// scanf("%c",&mapp[j][k][i]);
// if(mapp[j][k][i] == 'S')
// {
// a.x = j;
// a.y = k;
// a.z = i;
// }
// }
// getchar();
// }
// }
//}
int dir[6][3] = {{0,0,1},{0,0,-1},{-1,0,0},{0,1,0},{1,0,0},{0,-1,0}};//记录六种走法,方便遍历
int bfs()
{
queue<node>q;
struct node b;
a.step = 0;
q.push(a);
while(!q.empty())
{
a = q.front();
for(int i = 0; i < 6; i ++)
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
b.z = a.z + dir[i][2];
if(mapp[b.x][b.y][b.z] == '.')
{
b.step = a.step + 1;
mapp[b.x][b.y][b.z] = '#';
q.push(b);
}
else if(mapp[b.x][b.y][b.z] == 'E')
{
return a.step + 1;
}
}
q.pop();
}
return 0;
}
int main()
{
int x, y, z;
while(scanf("%d%d%d",&z,&x,&y)!=EOF)
{
if(x==0&&y==0&&z==0)break;
init();
// input(x, y, z);
for(int i = 1; i <= z; i++)
{
getchar();//第一次吸收输入完x,y,z之后的回车,之后吸收第每次输入时的多出来的空白行
for(int j = 1; j <= x; j ++)
{
for(int k = 1; k <= y; k ++)
{
scanf("%c",&mapp[j][k][i]);
if(mapp[j][k][i] == 'S')
{
a.x = j;
a.y = k;
a.z = i;
}
}
getchar();//每一个模块输入完后会有回车进入下一行
}
}
int ans = bfs();
if(ans>0)printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}