Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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?
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 5S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
【分析】這次的迷宮是立體的,有多層,故有6個方向(如下座標)從‘S’出發,
到‘E’點
【思路】1 採用BFS
2 注意6個方向
int dx[6] = { 0, 0, 1, -1, 0, 0};
int dy[6] = { 0, 0, 0, 0, 1, -1};
int dz[6] = {-1, 1, 0, 0, 0, 0};
//下 上 後 前 右 左
AC代碼:
#include <cstring>
using namespace std;
struct node
{
int x, y, z;
int step;
};
queue<node> q;
int L, R, C, sx, sy, sz, flag;
char map[31][31][31];
int visit[31][31][31];
int dx[6] = { 0, 0, 1, -1, 0, 0};
int dy[6] = { 0, 0, 0, 0, 1, -1};
int dz[6] = {-1, 1, 0, 0, 0, 0};
//下 上 後 前 右 左
int check(int x, int y, int z)//判斷座標是否越界
{
if(x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >=C)
return 0;
else
return 1;
}
void BFS()
{
while(!q.empty())//清空隊列
q.pop();
node s, e;//定義結構體的名稱
memset(visit, 0, sizeof(visit));
s.step = 0; s.x = sx; s.y = sy; s.z = sz;//出發
q.push(s);
visit[s.x][s.y][s.z] = 1;
while(!q.empty())
{
s = q.front();
q.pop();
if(map[s.x][s.y][s.z] == 'E')//判斷是否到達出口
{
cout << "Escaped in " << s.step << " minute(s)." << endl;
flag = 1;
}
for(int i = 0; i < 6; i++)
{
e.x = s.x + dx[i];
e.y = s.y + dy[i];
e.z = s.z + dz[i];
if(!check(e.x, e.y, e.z) || visit[e.x][e.y][e.z] || map[e.x][e.y][e.z] == '#')
continue;
e.step = s.step + 1;
q.push(e);
visit[e.x][e.y][e.z] = 1;
}
}
}
int main()
{
while(cin >> L >> R >> C)
{
if(L + R + C == 0) break;//程序結束條件
for(int i = 0; i < L; i++)
for(int j = 0; j < R; j++)
for(int k = 0; k < C; k++)
{
cin >> map[i][j][k];
if(map[i][j][k] == 'S')//尋找出發點
{ sx = i; sy = j; sz= k; }
}
flag = 0;
BFS();
if(!flag)
cout << "Trapped! "<< endl;
}
return 0;
}