您的位置:首页 > 其它

uva 532 - Dungeon Master

2012-07-04 00:33 495 查看
//uva 532 - Dungeon Master
//题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=105&problem=473&mosmsg=Submission+received+with+ID+10286403
//题目大意是三维的迷宫,S是起点E是终点,问能否走出去,能走出去的话就输出时间,否则输出"Trapped!"
//这题一开始用的深搜,结果竟然超时了,然后改广搜就过了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct Node{
int x, y, z;
} begin_node, end_node;
const int INF = 10000000;
queue<Node> node_que;
void BFS();
char map[50][50][50];
int time_record[50][50][50];
int direction[6][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
int high, row, column;
bool Inmap(Node);
int main(){
while(scanf("%d %d %d", &high, &row, &column), high || row || column){
getchar();
memset(map, 0, sizeof(map));
memset(time_record, -1, sizeof(time_record));
for (int i = 0; i < high; i++)
for (int j = 0; j <= row; j++)
gets (map[i][j]);
for (int i = 0; i < high; i++)
for (int j = 0; j < row; j++)
for (int k = 0; k < column; k++)
if(map[i][j][k] == 'S'){
begin_node.x = j;
begin_node.y = k;
begin_node.z = i;
} else
if (map[i][j][k] == 'E'){
end_node.x = j;
end_node.y = k;
end_node.z = i;
}
time_record[begin_node.z][begin_node.x][begin_node.y] = 0;
BFS();
if(time_record[end_node.z][end_node.x][end_node.y] == -1)
cout << "Trapped!" << endl;
else
printf("Escaped in %d minute(s).\n", time_record[end_node.z][end_node.x][end_node.y]);
}
return 0;
}
void BFS(){
Node now_node;
Node tmp_node;
node_que.push(begin_node);
while(node_que.size()){
now_node = node_que.front();
node_que.pop();
int now_node_time = time_record[now_node.z][now_node.x][now_node.y];
for(int i = 0; i < 6; i++){
tmp_node.x = now_node.x + direction[i][0];
tmp_node.y = now_node.y + direction[i][1];
tmp_node.z = now_node.z + direction[i][2];
int &next_node_time = time_record[tmp_node.z][tmp_node.x][tmp_node.y];
if(Inmap(tmp_node) && map[tmp_node.z][tmp_node.x][tmp_node.y] != '#' && (now_node_time + 1 < next_node_time || next_node_time == -1 )){
next_node_time = now_node_time + 1;
node_que.push(tmp_node);
}
}
}
}
bool Inmap(Node node){
return node.x >= 0 && node.y >= 0 && node.z >= 0 && node.x < row && node.y < column && node.z < high;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: