zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    

      

  • 相关阅读:
    python 线程同步
    python 线程模块
    Python线程
    Python 多线程
    Python SMTP发送邮件
    Python Internet 模块
    简单实例
    Socket 对象(内建)方法
    Python 网络编程
    python 数据库错误处理
  • 原文地址:https://www.cnblogs.com/acSzz/p/2378640.html
Copyright © 2011-2022 走看看