您的位置:首页 > 其它

POJ 2251宽搜、

2016-02-06 16:07 417 查看
因为这个题做了两次犯了两次不同的错误、

第一次用的dfs死活都超时

第二次把定义队列定义在了全局变量的位置,导致连WA了几次、最后找到原因的我真的想一巴掌拍死自己

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int qq=40;
int vis[qq][qq][qq];
char map[qq][qq][qq];
int tz,ty,tx,k,n,m,sx,sy,sz;
int dir[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,1,0,0,-1};
struct point{
int a,b,c;
int step;
};
int check(int z,int y,int x)
{
if(z<0||y<0||x<0||z>=k||y>=n||x>=m||vis[z][y][x]||map[z][y][x]=='#')
return 0;
return 1;
}
void bfs()
{
queue<point>Q;                    //就是这个定义队列放在了bfs函数外、导致我WA了很多次特地来提醒自己
memset(vis,0,sizeof(vis));        //在写宽搜的时候一定不要犯同样的错误了、
point now,ans;
now.step=0;now.a=sz;now.b=sy;now.c=sx;
vis[sz][sy][sx]=1;
Q.push(now);
while(!Q.empty()){
ans=Q.front();
Q.pop();
if(ans.a==tz&&ans.b==ty&&ans.c==tx){
printf("Escaped in %d minute(s).\n",ans.step);
return;
}
for(int i=0;i<6;++i){
now.a=ans.a+dir[i][0];
now.b=ans.b+dir[i][1];
now.c=ans.c+dir[i][2];
if (check(now.a,now.b,now.c))
{
now.step=ans.step+1;
vis[now.a][now.b][now.c]=1;
Q.push(now);
}
}
}
printf("Trapped!\n");
return;
}
int main()
{
while(~scanf("%d %d %d%*c",&k,&n,&m)&&k)
{
for(int i,j,l=0;l<k;++l){
for(j=0;j<n;++j){
for(i=0;i<m;++i){
map[l][j][i]=getchar();
if (map[l][j][i]=='S'){
sx=i;sy=j;sz=l;
}
else if (map[l][j][i]=='E'){
tx=i;ty=j;tz=l;
}
}
getchar();
}
getchar();
}
bfs();
}
return 0;
}


做搜索题目一定要思路清晰、然后是代码的意义要明白的透彻
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: