Dungeon Master
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 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题意: 有一个L层的地牢(像我们居住的楼房一样,有多层楼),每层都有R*C个房间,给出的输入中 ’#‘ 代表此房间走不通 ‘.’ 说明可以可以走,通过 ‘.’ 可以从一层楼传送到下一层或者是上一层,也可以在同一层楼中从一个 ’.’ 的房间穿到另外一个 ‘.’ 的房间。
即:走迷宫。不过迷宫的方向有6个,上、下、左、右、前、后。求:最短路径。
题解: bfs搜索即可,用队列来储存可以走的节点,最后把结果从终点往起点推,计算一下长度即可得出答案。这是一个简单题,详细信息见代码。
c++ AC 代码
#include<cstdio>
#include<cstdlib>
#include<queue>
int L, R, C; // 地图的三个维度
struct Node // 节点的结构体
{
Node * parent;
int x;
int y;
int z;
Node(int _x, int _y, int _z):x(_x),y(_y),z(_z) {}
};
const int dirictions[6][3] = {{0,1,0},{0,-1,0},{0,0,1},{0,0,-1},{1,0,0},{-1,0,0}};
int end_x,end_y,end_z;
bool is_in_map(int x, int y, int z);
int bfs(char(* maze)[35][35], Node * start);
int main()
{
while(scanf("%d%d%d",&L, &R, &C) && (R+L+C))
{
getchar();
char maze[35][35][35];
for(int k=0;k<L;k++)
{
for(int i=0;i<R;i++)
scanf("%s",maze[k][i]);
if(getchar() == '\n' && k!=(L-1))
getchar();
}
// 找到起点和终点坐标
Node * start;
for(int k = 0;k < L;k++)
for(int i = 0;i < R;i++)
for(int j = 0;j < C;j++)
{
if(maze[k][i][j] == 'S')
{
start = new Node(k,i,j);
start->parent = NULL;
}
else if(maze[k][i][j] == 'E')
{
end_x = k;
end_y = i;
end_z = j;
}
}
/* 这一部分是查看输入的
puts("\n");
for(int k=0;k<L;k++)
{
for(int i=0;i<R;i++)
printf("%s\n",maze[k][i]);
puts("");
}
system("pause");
*/
// bfs 搜索
int res = bfs(maze, start);
if(res) // 路径长度不是0
printf("Escaped in %d minute(s).\n",res);
else
puts("Trapped!");
}
system("pause");
return 0;
}
int bfs(char(* maze)[35][35], Node * start)
{
Node * end = new Node(NULL,NULL,NULL);
end->parent = NULL;
std::queue<Node *> coordinates_set;
coordinates_set.push(start);
// bfs 的部分
while(!coordinates_set.empty())
{
Node * cur_node = coordinates_set.front(); // 取出队列第一个节点
coordinates_set.pop();
for(int i=0;i<6;i++) // 探索该点上下左右前后6个点
{
int x, y, z;
x = dirictions[i][0] + cur_node->x;
y = dirictions[i][1] + cur_node->y;
z = dirictions[i][2] + cur_node->z;
if(!is_in_map(x,y,z)) // 超出地牢则继续探索下一个点
continue;
if(x == end_x && y == end_y && z == end_z) // 找到了则退出
{
end->x = x;
end->y = y;
end->z = z;
end->parent = cur_node;
break;
}
if(maze[x][y][z] == '.')
{
// printf("%d %d %d\n",x,y,z);
// system("pause");
maze[x][y][z] = '#'; // 走过的路标记做标记
Node * find_node = new Node(x,y,z);
find_node->parent = cur_node; // 该节点是从cur_node走过来的,记录它的父节点
coordinates_set.push(find_node); // 将该符合要求的节点入队
}
}
}
std::queue<Node *> res;
Node * node = end;
while(node->parent != NULL)
{
res.push(node); // 这个地方可以不用这个队列,题目并没有要我们输出路径,直接计算长度即可
node = node->parent;
}
return res.size();
}
bool is_in_map(int x, int y, int z) // 判断该点是否在地牢中
{
return 0 <= x && x < L && 0 <= y && y < R && 0 <= z && z < C;
}