POJ2251(三维BFS)
水题。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=35;
const double eps=1e-8;
const double PI = acos(-1.0);
int g[35][35][35],vis[35][35][35];
struct node
{
int x,y,z,step;
};
int x,y,z;
node s,e;
int dx[]={0,1,0,0,0,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,0,0,0,-1,0};
int bfs()
{
memset(vis,0,sizeof(vis));
queue<node> q;
q.push(s);
int ans=0;
while(!q.empty())
{
node t=q.front();
// cout<<t.step<<endl;
if(t.x==e.x&&t.y==e.y&&t.z==e.z)
{
ans=t.step;
break;
}
q.pop();
int xx=t.x,yy=t.y,zz=t.z;
for(int i=0;i<6;i++)
{
xx=t.x+dx[i];
yy=t.y+dy[i];
zz=t.z+dz[i];
if(xx>0&&xx<=x&&yy>0&&yy<=y&&zz<=z&&zz>0&&!vis[zz][xx][yy]&&g[zz][xx][yy]==1)
{
// cout<<"inf: "<<xx<<" "<<yy<<" "<<zz<<endl;
node u;
u.x=xx;
u.y=yy;
u.z=zz;
u.step=t.step+1;
q.push(u);
vis[zz][xx][yy]=1;
}
}
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
while(cin>>z>>x>>y)
{
if(!x&&!y&&!z)
break;
for(int i=1;i<=z;i++)
{
for(int j=1;j<=x;j++)
{
for(int k=1;k<=y;k++)
{
char c;
cin>>c;
if(c=='#')
g[i][j][k]=0;
else if(c=='.')
g[i][j][k]=1;
else if(c=='S')
{
g[i][j][k]=1;
s.x=j;
s.y=k;
s.z=i;
s.step=0;
}
else if(c=='E')
{
g[i][j][k]=1;
e.x=j;
e.y=k;
e.z=i;
e.step=-1;
}
}
}
// getchar();
}
/* for(int i=1;i<=z;i++)
{
for(int j=1;j<=x;j++)
{
for(int k=1;k<=y;k++)
{
cout<<g[i][j][k]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}*/
int ans=bfs();
if(!ans)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}