Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43911 Accepted: 16567
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!
t题意:有一个迷宫,起点为S终点为E,但是是3维的,有许多层,找到走到出口的最小步数
分析:宽度搜索题,只是将原先的是二维,这道题是三维,方向变为6个了,
ps:貌似求迷宫的最短路径一般都是用宽搜
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 35;
const int INF = 0x3f3f3f3f;
int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct Index
{
int x; //x轴
int y; //y轴
int z; //z轴
int s; //到这个坐标的步数
};
char map[MAXN][MAXN][MAXN];
bool used[MAXN][MAXN][MAXN]; //若used数组值为真,则表示可以用此坐标
int l, r, c;
Index end;
int bfs(Index start)
{
queue<Index> que;
memset(used, true, sizeof(used)); //初始化所有位置 默认全部都是无法到达的(true)
que.push(start); //将起点压入队列
while (!que.empty()) {
Index tempI = que.front(); que.pop(); //从队列的最前端取出元素
if (tempI.z == end.z && tempI.x == end.x && tempI.y == end.y) { //如果取出的状态已经是终点则结束搜索
return tempI.s;
}
Index loc; //移动后的位置记为loc
for (int i = 0; i < 6; i++) { //六个方向的循环
loc.z = tempI.z + dir[i][0];
loc.x = tempI.x + dir[i][1];
loc.y = tempI.y + dir[i][2];
//因为判断条件太多所以用两个if,判断是否已经访问过了(若未访问dis[loc.z][loc.x][loc.y] == INF)
if (loc.x >= 0 && loc.x < r && loc.y >= 0 && loc.y < c && loc.z >= 0 && loc.z < l) {
if (map[loc.z][loc.x][loc.y] != '#' && used[loc.z][loc.x][loc.y]) { //若可以移动的话,则加入队列,并且距离+1
loc.s = tempI.s + 1;
que.push(loc);
used[loc.z][loc.x][loc.y] = false;
}
}
}
}
return INF; //运行到这里表示无法到达,返回INF
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c) && l) {
int step_num; //起点到终点的步数如果step_num==INF则表示终点无法到达
Index start;
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]; //用c++输入比用c输入,可以很好的处理字符串输入问题
if (map[i][j][k] == 'S') {
start.x = j;
start.y = k;
start.z = i;
start.s = 0;
}
if (map[i][j][k] == 'E') {
end.x = j;
end.y = k;
end.z = i;
}
}
}
}
// printf("%d%d%d\n", end.z, end.x, end.y);
step_num = bfs(start);
if (step_num == INF) {
printf("Trapped!\n");
}
else {
printf("Escaped in %d minute(s).\n", step_num);
}
}
return 0;
}