0
点赞
收藏
分享

微信扫一扫

迷宫 (bfs 逆序建图 三维压缩

Soy丶sauce 2022-04-05 阅读 70

添加链接描述

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+9;
int dist[N];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
struct node
{
    int a,b,c;
};
int n,m,h,Q;
int get(int a,int b,int c){
    return (c)*n*m+(b)*n+a;//!!
}
int main(){
    int T;
    cin>>T;
    while(T--){
        queue<node>q;
        
        cin>>n>>m>>h>>Q;
        memset(dist,0x3f,sizeof dist);
        while(Q--){
            int op,a,b,c;
            cin>>op>>a>>b>>c;
            int p=get(a,b,c);
            if(p>1e7)assert(0);
            if(op==1)dist[p]=0,q.push({a,b,c});
            else {
                while(q.size()){
                    auto x=q.front();
                    q.pop();
                    for(int i=0;i<6;i++){
                        int nx=x.a+dx[i];
                        int ny=x.b+dy[i];
                        int nz=x.c+dz[i];
                        if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&nz>=1&&nz<=h){
                            if(dist[get(nx,ny,nz)]>dist[get(x.a,x.b,x.c)]+1){
                                dist[get(nx,ny,nz)]=dist[get(x.a,x.b,x.c)]+1;
                                q.push({nx,ny,nz});
                            }
                        }
                    }
                }
                cout<<dist[p]<<"\n";
            }
        }
    }



    return 0;
}
举报

相关推荐

0 条评论