UVA 532-Dungeon Master
题目大意:给出一个三维图,#代表障碍物,E代表出口,S代表初始位置,求离开的最短路
解题思路:三维数组bfs求最短路
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct dun {
int x, y, z, m;
};
int l, h, w;
int ch[50][50][50];
int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
queue<dun> que;
int s;
int bfs() {
while(!que.empty()) {
for(int i = 0; i < 6; i++) {
int a = que.front().z + dir[i][0], b = que.front().x + dir[i][1], c = que.front().y + dir[i][2];
if(a >= 0 && a < l && b >= 0 && b < h && c >= 0 && c < w){
if(ch[a][b][c] == 2)
return que.front().m + 1;
else if(ch[a][b][c] == 0) {
dun p;
p.z = a;
p.x = b;
p.y = c;
p.m = que.front().m + 1;
que.push(p);
ch[a][b][c] = 1;
}
}
}
que.pop();
}
return -1;
}
int main() {
while(cin >> l >> h >> w && l + h + w != 0) {
memset(ch, 0, sizeof(ch));
while(!que.empty())
que.pop();
char str[35];
getchar();
for(int i = 0; i < l; i++) {
for(int j = 0; j < h; j++) {
gets(str);
for(int k = 0; k < w; k++) {
if(str[k] == 'S'){
dun g;
g.x = j;
g.y = k;
g.z = i;
g.m = 0;
ch[i][j][k] = 1;
que.push(g);
}
else if(str[k] == 'E') {
ch[i][j][k] = 2;
}
else if(str[k] == '#')
ch[i][j][k] = 1;
}
}
gets(str);
}
s = bfs();
if(s == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", s);
}
return 0;
}