描述
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?
你被困在一个3D 地牢,需要找到最快的出路!地牢是由单位立方体组成的,这些立方体可能被岩石填满,也可能不被岩石填满。向北、向南、向东、向西、向上或向下移动一个单元需要一分钟。你不能沿着对角线移动,迷宫的四周都是坚硬的岩石。有可能逃跑吗?如果是,需要多长时间?
输入
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、 R 和 C (所有这些整数的大小都限制在30)。L 是组成地牢的层数。R 和 C 是构成每个级别的计划的行数和列数。然后,将出现由 R 行组成的 L 块,每行包含 C 字符。每个角色都描述了地牢的一个细胞。装满岩石的单元格用“ #”表示,空单元格用“’”表示.你的起始位置用“ S”表示,出口用字母“ E”表示。每一关后面都有一行空白。对于 L、 R 和 C,输入以三个零结束。
输出
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!
每个迷宫产生一行输出。如果可以到达出口,输出:Escaped in x minute(s).。其中 x 被逃逸所需的最短时间所代替。如果无法逃脱,输出Trapped!
样例输入
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
样例输出
Escaped in 11 minute(s). Trapped!
来源
Ulm Local 1997
使用了翻译器,有些语句不通顺请谅解,谢谢支持
分析
用BFS
也可以用寻路,这和普通的寻路一样,只要把地图的二维数组变成三维就可以
代码
我用的BFS
#include <iostream>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
string s[31][31];
int vis[31][31][31];
int L, n, m;
int arr[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {0, 1, 0}, {-1, 0, 0}, {1, 0, 0}};
struct node
{
int x, y, z, t;
};
int bfs(int x, int y, int z)
{
node t1, t2;
vis[x][y][z] = 1;
queue<node> q;
q.push({x, y, z, 0});
while (!q.empty())
{
t1 = q.front();
q.pop();
for (int i = 0; i < 6; i++)
{
t2.x = t1.x + arr[i][0];
t2.y = t1.y + arr[i][1];
t2.z = t1.z + arr[i][2];
t2.t = t1.t + 1;
if ((t2.x >= 0 && t2.x < L) && (t2.y >= 0 && t2.y < n) && (t2.z >= 0 && t2.z < m) && ((s[t2.x][t2.y][t2.z] == 'E' || s[t2.x][t2.y][t2.z] == '.') && !vis[t2.x][t2.y][t2.z]))
{
if (s[t2.x][t2.y][t2.z] == 'E')
{
return t2.t;
}
vis[t2.x][t2.y][t2.z] = 1;
q.push(t2);
}
}
}
return -1;
}
int main()
{
while (1)
{
int ans;
cin >> L >> n >> m;
memset(vis, 0, sizeof(vis));
if (L == 0 && n == 0 && m == 0)
break;
for (int i = 0; i < L; i++)
{
for (int j = 0; j < n; ++j)
{
cin >> s[i][j];
}
}
for (int i = 0; i < L; i++)
{
for (int j = 0; j < n; ++j)
{
for (int p = 0; p < m; ++p)
{
if (s[i][j][p] == 'S')
{
ans = bfs(i, j, p);
break;
}
}
}
}
if (ans == -1)
cout << "Trapped!";
else
printf("Escaped in %d minute(s).", ans);
cout << endl;
}
return 0;
}
给个赞和关注吧