| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 20450 | Accepted: 7917 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
一个简单的BFS找最短路径,只不过背景是三维的罢了。。
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int mp[35][35][35];
int vis[35][35][35];
//往六个方向走
const int dx[] = {1, 0, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, 0, -1, 0};
const int dz[] = {0, 0, 1, 0, 0, -1};
int L, R, C;
int sx, sy, sz;
int ans;
struct node {
int x, y, z;
int c;
node(int _x, int _y, int _z, int _c) {
x = _x;
y = _y;
z = _z;
c = _c;
}
};
bool bfs(int x, int y, int z) {
memset(vis, 0, sizeof(vis));
queue<node> que;
que.push(node(x, y, z, 0));
vis[x][y][z] = 1;
while(!que.empty()) {
node t = que.front();
que.pop();
for(int i = 0; i < 6; i ++) {
int xx = t.x + dx[i];
int yy = t.y + dy[i];
int zz = t.z + dz[i];
if(xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && zz < C && mp[xx][yy][zz] != 1
&& !vis[xx][yy][zz]) { //好吧,<L,<R,<C统统写成<=了,莫名其妙跪啦好久,傻了。。
if(mp[xx][yy][zz] == 4) {
ans = t.c + 1;
return true;
}
que.push(node(xx, yy, zz, t.c + 1));
vis[xx][yy][zz] = 1;
}
}
}
return false;
}
int main() {
while(scanf("%d %d %d", &L, &R, &C) != EOF) {
if(L == 0 && R == 0 && C == 0) break;
for(int i = 0; i < L; i ++) {
for(int j = 0; j < R; j ++) {
char str[55];
scanf("%s", str);
for(int k = 0; k < C; k ++) {
if(str[k] == '.') {
mp[i][j][k] = 0;
}
else if(str[k] == '#') {
mp[i][j][k] = 1;
}
else if(str[k] == 'S') {
mp[i][j][k] = 3;
sx = i; sy = j; sz = k;
}
else if(str[k] == 'E') {
mp[i][j][k] = 4;
}
}
}
}
// for(int i = 0; i < L; i ++, printf("\n")) {
// for(int j = 0; j < R; j++, printf("\n")) {
// for(int k = 0; k < C; k ++) {
// printf("%d", mp[i][j][k]);
// }
// }
// }
ans = 0;
if(bfs(sx, sy, sz)) {
printf("Escaped in %d minute(s).\n", ans);
}
else {
printf("Trapped!\n");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ - 2251 - Dungeon Master (简单BFS)
原文地址:http://blog.csdn.net/u014355480/article/details/47053941