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;}



原创粉丝点击