poj 3278 Catch That Cow
来源:互联网 发布:淘宝上的ica麦片真货 编辑:程序博客网 时间:2024/06/03 20:32
题目链接:点击打开链接
比赛的时候wa了十次。。。。
因为那个网速太坑爹,我对BFS搜索的题注意点也不是很熟悉。。。。
每次只知道没过,看不到提交结果。。。凭着感觉改终于改对了。。。
注意点:
1、写那个能不能走的判断条件时要把 越界的条件 写在 是否访问判断的前面,利用&&
如果前面不成立,后面就不判断的性质。这样才不会下标越界,出现RE。
2、数组只开到了100010,我判断越界的条件写成if(x>=0 && x<=100010 &&。。。)多了等于号。
导致下标越界。
凭着感觉改了10次终于AC的代码:(罚时把我害惨了。。。。)
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<queue>#include<stack>#include<algorithm>#include<vector>#include<cmath>#include<cstdlib>#include<cctype>using namespace std;int n,k;bool vis[100010];struct node{ int step; int id;}t;queue<node> q;int bfs(){ while(!q.empty()) q.pop(); t.id=n; t.step=0; q.push(t); vis[t.id]=true; node cur,next1,next2,next3; while(!q.empty()) { cur=q.front(); q.pop(); if(cur.id==k) return cur.step; next1.id=cur.id+1; if(next1.id>=0 && next1.id <100010) { if(!vis[next1.id]) { next1.step=cur.step+1; q.push(next1); vis[next1.id]=true; } } next2.id=cur.id-1; if(next2.id>=0 && next2.id <100010) { if(!vis[next2.id]) { next2.step=cur.step+1; q.push(next2); vis[next2.id]=true; } } next3.id=cur.id*2; if( next3.id>=0 && next3.id <100010) { if(!vis[next3.id]) { next3.step=cur.step+1; q.push(next3); vis[next3.id]=true; } } }}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { memset(vis,0,sizeof(vis)); long long ans=bfs(); printf("%I64d\n",ans); } return 0;}
最后一次WA的代码
只是去掉了<=100010中越界判断的等号就AC了。。。。我数组只开了100010啊。。。。
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<queue>#include<stack>#include<algorithm>#include<vector>#include<cmath>#include<cstdlib>#include<cctype>using namespace std;int n,k;bool vis[100010];struct node{ int step; int id;}t;queue<node> q;int bfs(){ while(!q.empty()) q.pop(); t.id=n; t.step=0; q.push(t); vis[t.id]=true; node cur,next1,next2,next3; while(!q.empty()) { cur=q.front(); q.pop(); next1.id=cur.id+1; if(next1.id>=0 && next1.id <100010 && !vis[next1.id] ) { next1.step=cur.step+1; if(next1.id==k) return next1.step; else { q.push(next1); vis[next1.id]=true; } } next2.id=cur.id-1; if(next2.id>=0 && next2.id <100010 && !vis[next2.id]) { next2.step=cur.step+1; if(next2.id==k) return next2.step; else { q.push(next2); vis[next2.id]=true; } } next3.id=cur.id*2; if( next3.id>=0 && next3.id <100010 && !vis[next3.id] ) { next3.step=cur.step+1; if(next3.id==k) return next3.step; else { q.push(next3); vis[next3.id]=true; } } }}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { memset(vis,0,sizeof(vis)); long long ans=bfs(); printf("%I64d\n",ans); } return 0;}
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 catch that cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 - Catch That Cow
- POJ 3278 Catch That Cow
- POJ 3278 Catch That Cow
- POJ-3278-Catch That Cow
- 音视频相关参数总结
- Hibernate 的 10 个常见面试问题及答案
- 最牛B的编码套路
- 深入分析一道基础面试题
- SCANV团队:警惕邮编区号查询工具的高危漏洞
- poj 3278 Catch That Cow
- 内核对象与句柄的关系
- linux input 子系统分析 一
- sbull在新内核编译
- [Linux] 防火墙配置
- Linux shell中函数的定义和使用
- 离散傅立叶变换之听声音破解电话号码
- linux input 子系统分析 二
- 长点心吧