题目
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 lineTrapped!
Sample
Inputcopy Outputcopy 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
思路 BFS
这题是一个多维的BFS与HDU-2102类似,思路就不说了.
令人悲伤的是我叫了很多次,血书上的教训并没有被记住,以后补题还是尽量早补早总结!
Code
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <utility>
// #include <unordered_set>
// #include <unordered_map>
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);
#define rep(i, j, k) for(int i = j; i <= k; ++ i)
#define per(i, j, k) for(int i = j; i >= k; -- i)
#define dbg1(a) cout << a << endl;
#define dbg2(a, b) cout << a << " " << b << endl;
#define dbg3(a, b, c) cout << a << " " << b << " " << c << endl;
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define lc p<<1
#define rc p<<1|1
using namespace std;
// typedef long long LL;
// typedef priority_queue<int, vector<int>, greater<int>> S_HEAP;
// typedef priority_queue<int> B_HEAP;
// typedef pair<string, int> PSI;
// typedef pair<int, int> PII;
const int N = 1e5 + 10;
int L, R, C;
int dx[] = {0, 1, -1, 0, 0, 0}, dy[] = {1, 0, 0, -1, 0, 0}, dz[] = {0, 0, 0, 0, 1, -1};
char a[35][35][35];
int d[35][35][35];
struct tuples {
int z, x, y;
};
void bfs(int sz, int sx, int sy) {
memset(d, -1, sizeof d);
queue<tuples> q1;
tuples n1; n1.x = sx, n1.y = sy, n1.z = sz;
q1.push(n1);
d[sz][sx][sy] = 0;
while (q1.size()) {
tuples t = q1.front(); q1.pop();
int z = t.z, x = t.x, y = t.y;
if (a[z][x][y] == 'E') {
printf("Escaped in %d minute(s).\n", d[z][x][y]);
return;
}
//cout << z << x << y << endl;
rep (i, 0, 5) {
int tx = x + dx[i], ty = y + dy[i], tz = z + dz[i];
if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && tz <= L && tz >= 1 && d[tz][tx][ty] == -1 && a[tz][tx][ty] != '#') {
d[tz][tx][ty] = d[z][x][y] + 1;
tuples n1; n1.z = tz, n1.x = tx, n1.y = ty;
q1.push(n1);
}
}
}
puts("Trapped!");
return;
}
int main() {
int sx, sy, sz;
while (cin >> L >> R >> C, L || R || C) {
rep (i, 1, L) {
rep (j, 1, R) {
rep (k, 1, C) {
cin >> a[i][j][k];
if (a[i][j][k] == 'S') {
sx = j, sy= k, sz = i;
}
}
}
}
bfs(sz, sx, sy);
}
}