poj 3278 Catch That Cow (bfs 搜索)

一直很晕,用c++16Ms过,用G++WA!!!!!!!!

http://poj.org/problem?id=3278

#include<stdio.h>

#include<string.h>

#define N 100000

int n,k,vis[N];

struct node

{

    int step;

    int num;

}p[N*10];

int  bfs()

{

    memset(vis,0,sizeof(vis));

    int head=0,tail=0;

    p[head].num=n;

    p[head].step=0;

    vis[n]=1;

    tail++;

    while(head<tail)

    {

        int l=p[head].num;

        if(l==k)return p[head].step;

          if(p[head].num+1<=N&&!vis[p[head].num+1])

          {

              p[tail].num=p[head].num+1;

              vis[p[head].num+1]=1;

              p[tail].step=p[head].step+1;

              tail++;

          }





         if(p[head].num-1>=0&&!vis[p[head].num-1])

         {

              p[tail].num=p[head].num-1;

              vis[p[head].num-1]=1;

              p[tail].step=p[head].step+1;

             tail++;

         }

         if(p[head].num*2<=N&&!vis[p[head].num*2])

         {

             p[tail].num=p[head].num*2;

             vis[p[head].num*2]=1;

         p[tail].step=p[head].step+1;

         tail++;

         }





         head++;





    }

    return 0;

}

int main()

{



    while(scanf("%d%d",&n,&k)!=EOF)

    {

       int ans= bfs();

       printf("%d\n",ans);

    }

}

  

你可能感兴趣的:(catch)