您的位置:首页 > 其它

POJ_2251_Dungeon Master

2010-06-22 12:42 316 查看
////三维迷宫求最短路径,BFS,三维的状态空间
///提交WA两次,一次忘记将栈清空,一次忘记把本地测试的语句删掉
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char dungeon[35][35][35];
int f[35][35][35];
int dr[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int l,r,c;
queue<int> q;
int bfs(int a,int b,int d)
{
memset(f,-1,sizeof(f));
f[a][b][d]=0;
int i,j,k;
q.push(a);
q.push(b);
q.push(d);
while(!q.empty())
{
int m,n,p;
m=q.front();q.pop();
n=q.front();q.pop();
p=q.front();q.pop();
for(i=0;i<6;i++)
{
int x,y,z;
x=m+dr[i][0];
y=n+dr[i][1];
z=p+dr[i][2];
if(x<0||x>=l)
continue;
if(y<0||y>=r)
continue;
if(z<0||z>=c)
continue;
if(dungeon[x][y][z]=='.')
{
if(f[x][y][z]==-1)
{
f[x][y][z]=f[m]
[p]+1;
q.push(x);q.push(y);q.push(z);
}
}
if(dungeon[x][y][z]=='E')
{
f[x][y][z]=f[m]
[p]+1;
return f[x][y][z];
}
}
}
return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d%d",&l,&r,&c)==3)
{
if(!l&&!r&&!c)
break;
int i,j,k,t=0;
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
scanf("%s",dungeon[i][j]);
}
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
for(k=0;k<c;k++)
{
if(dungeon[i][j][k]=='S')
{
t=1;
break;
}
}
if(t)break;
}
if(t)break;
}
k=bfs(i,j,k);
if(k)
printf("Escaped in %d minute(s)./n",k);
else
printf("Trapped!/n");
while(!q.empty())
q.pop();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: