其他分享
首页 > 其他分享> > 蓝桥杯-地牢大师

蓝桥杯-地牢大师

作者:互联网

链接:https://www.acwing.com/problem/content/1098/

思路:1:三维bfs:可以分为当前层数内的二维内偏移和层数的一维偏移

代码:

#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
int l,r,c;
char s[105][105][105];
int b[105][105][105];
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
int dz[]={-1,1};
struct node{
    int x,y,z;
    node(int _z,int _x,int _y):x(_x),y(_y),z(_z){}
};

void bfs(int z,int x,int y){
    int ta,tb,tc;
     queue<node>q;
     q.push(node(z,x,y));
     memset(b,-1,sizeof(b));
     b[z][x][y]=1;
     while(q.size()){
        x=q.front().x;
        y=q.front().y;
        z=q.front().z;

        q.pop();
        for(int i=0;i<4;i++){
                ta=z;
                tb=x+dx[i];
                tc=y+dy[i];
                if(ta<1||tb<1||tc<1||ta>l||tb>r||tc>c||s[ta][tb][tc]=='#')continue;
                if(b[ta][tb][tc]!=-1)continue;
               // cout<<ta<<" "<<tb<<" "<<tc<<endl;
                b[ta][tb][tc]=b[z][x][y]+1;
                q.push(node(ta,tb,tc));
                if(s[ta][tb][tc]=='E')return;
        }
      for(int i=0;i<2;i++){
                ta=z+dz[i];
                tb=x;
                tc=y;
                if(ta<1||tb<1||tc<1||ta>l||tb>r||tc>c||s[ta][tb][tc]=='#')continue;
                if(b[ta][tb][tc]!=-1)continue;

                //cout<<ta<<" "<<tb<<" "<<tc<<endl;
                b[ta][tb][tc]=b[z][x][y]+1;
                q.push(node(ta,tb,tc));
                if(s[ta][tb][tc]=='E')return;
      }
     }
}
int main (){
    int x,y,z;
    while(cin>>l>>r>>c&&(l+r+c)){
        for(int t=1;t<=l;t++){
            for(int i=1;i<=r;i++)
                cin>>(s[t][i]+1);
        }
        for(int t=1;t<=l;t++)
            for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
            if(s[t][i][j]=='S')bfs(t,i,j);
            else if(s[t][i][j]=='E'){
                x=t;
                y=i;
                z=j;
            }
        if(b[x][y][z]==-1)cout<<"Trapped!"<<endl;
        else cout<<"Escaped in "<<b[x][y][z]-1<<" minute(s)."<<endl;
    }


    return 0;
}

标签:地牢,include,int,ta,蓝桥,105,tb,tc,大师
来源: https://www.cnblogs.com/abestxun/p/14661402.html