Acwing 1096. 地牢大师(三维BFS)
作者:互联网
三维BFS啦~之前没见过,长长见识,注意三维的偏移量的标法
#include <cstdio>
#include <cstring>
#include <iostream>
#include<queue>
#include <algorithm>
using namespace std;
struct node
{
int x,y,z;
int d;
};
const int N = 105;
int n,m,h;
char g[N][N][N];
bool vis[N][N][N];
int dx[]={0,1,0,-1,0,0};
int dy[]={1,0,-1,0,0,0};
int dz[]={0,0,0,0,1,-1};
int x1,y1,z1,x2,y2,z2;
int bfs()
{
queue<node> q;
q.push({x1,y1,z1,0});
memset(vis,false,sizeof vis);
vis[z1][x1][y1]=true;
while(!q.empty())
{
node t=q.front();q.pop();
int x=t.x,y=t.y,z=t.z,d=t.d;
if(x==x2&&y==y2&&z==z2) return d;
for(int i=0;i<6;i++)
{
int nx=x+dx[i],ny=y+dy[i],nz=z+dz[i];
if(nx<1||nx>n||ny<1||ny>m||nz<1||nz>h) continue;
if(g[nz][nx][ny]=='#') continue;
if(vis[nz][nx][ny]) continue;
vis[nz][nx][ny]=true;
q.push({nx,ny,nz,d+1});
}
}
return -1;
}
int main()
{
while(cin>>h>>n>>m&&h&&n&&m)
{
for(int i=1;i<=h;i++)
for(int j=1;j<=n;j++)
cin>>g[i][j]+1;
for(int i=1;i<=h;i++)
for(int j=1;j<=n;j++)
for(int z=1;z<=m;z++)
if(g[i][j][z]=='S')
{
z1=i;x1=j;y1=z;
}
else if(g[i][j][z]=='E')
{
z2=i;x2=j;y2=z;
}
int res=bfs();
if(res==-1) cout<<"Trapped!"<<endl;
else cout<<"Escaped in "<<res<<" minute(s)."<<endl;
}
}
标签:1096,ny,int,BFS,vis,nz,&&,include,Acwing 来源: https://blog.csdn.net/weixin_50533561/article/details/122791213