poj 2251
B - Dungeon Master
简单bfs,不是第一次做了,犯了俩错误
chukou开始不小心写成了bool类
然后queue一定要在每次清空(写在bfs函数里面就行)
不然上一次没扔出来的下次被优先访问出问题。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
int l,r,c;
int chukou=0;
int dungeon[32][32][32];//0是空,1是已经访问,2是被堵住
int start_l,start_r,start_c,end_l,end_r,end_c;
using namespace std;
struct node
{
int node_l;
int node_r;
int node_c;
int bushu;
}x,x1;
void init()
{
chukou=0;
for(int i=0;i<l;i++)
{
getchar();
for(int j=0;j<r;j++)
{
for(int z=0;z<c;z++)
{
char n;
scanf("%c",&n);
if(n=='#')
dungeon[i][j][z]=2;
else if(n=='.')
dungeon[i][j][z]=0;
else if(n=='S')
{
start_l=i;
start_r=j;
start_c=z;
}
else if(n=='E')
{
end_l=i;
end_r=j;
end_c=z;
}
}
getchar();
}
}
}
void shuchu()
{
for(int i=0;i<l;i++)
{
for(int j=0;j<r;j++)
{
for(int z=0;z<c;z++)
printf("%d ",dungeon[i][j][z]);
printf("\n");
}
printf("\n");
}
}
void xunlu()
{
queue<node> que1;
chukou=0;
int zan_l,zan_r,zan_c;
int to[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
node m;
m.node_l=start_l;
m.node_r=start_r;
m.node_c=start_c;
m.bushu=0;
que1.push(m);
int now_l,now_r,now_c;
while(!que1.empty() && chukou==0)
{
x=que1.front();
que1.pop();
zan_l=x.node_l;
zan_r=x.node_r;
zan_c=x.node_c;
for(int i=0;i<6;i++)
{
now_l=zan_l+to[i][0];
now_r=zan_r+to[i][1];
now_c=zan_c+to[i][2];
if(now_l==end_l && now_r==end_r && now_c==end_c)
{
//cout<<x.bushu<<endl;
//cout<<x.bushu+1<<endl;
chukou=x.bushu+1;
//cout<<chukou<<"daan"<<endl;
//cout<<x.node_l<<x.node_r<<x.node_c<<"zuobiao"<<endl;
}
if(dungeon[now_l][now_r][now_c]==0 && now_l>=0 && now_l<l && now_r>=0 && now_r<r && now_c>=0 && now_c<c)
{
dungeon[now_l][now_r][now_c]=1;
m.node_l=now_l;
m.node_r=now_r;
m.node_c=now_c;
m.bushu=x.bushu+1;
que1.push(m);
//cout<<"点"<<m.node_l<<m.node_r<<m.node_c<<" "<<m.bushu<<endl;
}
}
}
if(chukou==0)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<chukou<<" minute(s)."<<endl;
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c) && (l!=0||r!=0||c!=0))
{
init();
xunlu();
}
return 0;
}