POJ 2251宽搜、
2016-02-06 16:07
417 查看
因为这个题做了两次犯了两次不同的错误、
第一次用的dfs死活都超时
第二次把定义队列定义在了全局变量的位置,导致连WA了几次、最后找到原因的我真的想一巴掌拍死自己
做搜索题目一定要思路清晰、然后是代码的意义要明白的透彻
第一次用的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; }
做搜索题目一定要思路清晰、然后是代码的意义要明白的透彻
相关文章推荐
- 广搜练习(一)
- UVa--10976 Fractions Again(枚举)
- hdu1224 Free DIY Tour (dp)
- 业务流程节点信息提示
- 泛型与集合类型
- 业务流程节点信息构件
- 寻找山顶
- javascript+css3 实现动态按钮菜单特效
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
- POJ 2104 K-th Number
- 【Android】5.0 第5章 常用基本控件--本章示例主界面
- poj--2236--Wireless Network(并查集)
- Java [Leetcode 257]Binary Tree Paths
- poj--2236--Wireless Network(并查集)
- 短信验证码服务
- 鸡兔同笼问题
- 三消游戏核心逻辑的一种实现
- 二叉树(二)
- 【玩转cocos2d-x之四十】怎样在Cocos2d-x 3.0中使用opengl shader?