uva 532 - Dungeon Master

//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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值