0223B

1 篇文章 0 订阅
1 篇文章 0 订阅
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <iomanip>
#include <cmath>
#include <set>
#include <map>
#include <queue>

using namespace std;

int row,col,heit;
int g[40][40][40];
int dia[6][3]={0,1,0,0,-1,0,0,0,-1,0,0,1,-1,0,0,1,0,0};
int vis[40][40][40];

struct node{
    int r,c,h;
};

node s,e;

node md(int _h,int _r,int _c){
    node Node;
    Node.c=_c;
    Node.h=_h;
    Node.r=_r;
    return Node;
}

void init(){
    memset(vis,-1,sizeof vis);
}

void bfs(){
    queue<node>q;
    while (!q.empty())q.pop();
    q.push(s);
    vis[s.h][s.r][s.c] = 0;
    while (!q.empty()){
        node temp = q.front();
        q.pop();
    //  cout  << "lalalademaxiya!!"<<temp.h <<" "<<temp.r <<" "<<temp.c<<endl;
        for (int i = 0;i<6;++i){
            //int dia[6][3]={0,1,0,0,-1,0,0,0,-1,0,0,1,-1,0,0,1,0,0};
            int dh = temp.h + dia[i][0],dr = temp.r + dia[i][1],dc = temp.c + dia[i][2];
        //  cout << dh <<" "<< dr <<" "<< dc <<" "<< endl;
            if (dh <= 0 || dr <=0 || dc <=0 || dr > row || dh >heit || dc > col )continue;

            if (g[dh][dr][dc]==1 && vis[dh][dr][dc]==-1){
                vis[dh][dr][dc] = vis[temp.h][temp.r][temp.c] + 1;
                q.push(md(dh,dr,dc));
            //  cout << dh <<" "<<dr<<" "<<dc<<endl;
            }
        }
    }
}

int main(){
//  freopen("t.x","w",stdout);
    while (cin >> heit >> row >>col ){
        if (!row&&!col&&!heit){
            return 0;
        }
        for (int i = 1;i<=heit;++i){
            for (int j=1;j<=row;++j){
                for (int k=1;k<=col;++k){
                    char c;
                    cin >> c;
                    if (c == '\n'||c=='\t')cin >>c >> c;
                    if (c=='#')g[i][j][k] = 0;
                    if (c=='.')g[i][j][k] = 1;
                    if (c=='S'){
                        s = md(i,j,k);
                        g[i][j][k] = 1;
                    }
                    if (c=='E'){
                        e=md(i,j,k);
                        g[i][j][k] = 1;
                    }
                }
            }
        }
        init();
        bfs();
        if(vis[e.h][e.r][e.c]>-1) cout << "Escaped in "<<vis[e.h][e.r][e.c]<<" minute(s).\n";else cout<<"Trapped!\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值