poj 2251 Dungeon Master( bfs )

时间:2023-03-09 16:01:17
poj 2251 Dungeon Master( bfs )

题目:http://poj.org/problem?id=2251

简单三维 bfs不解释, 1A,     上代码

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; int a,b,c;
int vis[][][],G[][][];
int dx[]={,,,,-,};
int dy[]={,,,-,,};
int dz[]={,-,,,,};
struct node
{
int l,r,c,step;
}e,s,pos,next;
int bfs(int l,int r,int c)
{
int i;
queue<node>q;
next.l=l; next.r=r; next.c=c; next.step=;
vis[l][r][c]=;
q.push(next);
while(!q.empty())
{
pos=q.front();
q.pop();
for(i=; i<; i++)
{
next.l=pos.l+dx[i]; next.r=pos.r+dy[i];
next.c=pos.c+dz[i]; next.step=pos.step+;
if(!vis[next.l][next.r][next.c]&&G[next.l][next.r][next.c])
{
vis[next.l][next.r][next.c]=;
q.push(next);
}
if(next.l==s.l&&next.r==s.r&&next.c==s.c)
return next.step;
}
}
return -;
}
int main()
{
int i,j,k,x;
char ch;
while(cin>>a>>b>>c&&(a!=||b!=||c!=))
{
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
for(i=; i<=a; i++)
for(j=; j<=b; j++)
for(k=; k<=c; k++)
{
cin>>ch;
if(ch=='.')
G[i][j][k]=;
if(ch=='E')
{
e.l=i; e.r=j; e.c=k;
G[i][j][k]=;
}
if(ch=='S')
{
s.l=i; s.r=j; s.c=k;
G[i][j][k]=;
}
}
x=bfs(e.l,e.r,e.c);
if(x==-)
cout<<"Trapped!"<<endl;
else
printf("Escaped in %d minute(s).\n",x);
}
return ;
}