zoukankan      html  css  js  c++  java
  • 抓住那头牛

    广度优先搜索,深搜超时
    需要判断界限,做标记,已到过的地点就不必去了

    #include<cstdio>
    #include<iostream>
    using namespace std;
    int head=0,tail=1;
    int n,k;
    int f[199999];
    int d[9999999];
    int t[9999999];
    int bfs(int w)
    {
        d[1]=w;
        while(head<tail)
        {
            head++;int x;
            for(int i=1;i<=3;i++)
            {
    
                if(i==1) x=d[head]+1;
                if(i==2) x=d[head]-1;
                if(i==3) x=d[head]*2;
                if(f[x]==0&&x>=0&&x<=100000)
            {
                f[x]=1;
                d[++tail]=x;
            t[tail]=t[head]+1;if(d[tail]==k) return t[tail];
            }
            }
    
            /*if(f[d[head]+1]==0&&d[head]+1>0&&d[head]+1<100000)
            {
                f[d[head]+1]=1;
                d[++tail]=d[head]+1;
            t[tail]=t[head]+1;if(d[tail]==k) return t[tail];
            }
            if(f[d[head]-1]==0&&d[head]-1>0&&d[head]-1<100000)
            {
                f[d[head]-1]=1;
            d[++tail]=d[head]-1;
            t[tail]=t[head]+1;if(d[tail]==k) return t[tail];}
            if(f[d[head]*2]==0&&d[head]*2>0&&d[head]*2<100000)
            {
                f[d[head]*2]=1;
            d[++tail]=d[head]*2;
            t[tail]=t[head]+1;if(d[tail]==k) return t[tail];}*/
        }
        return 0;
    }
    int main()
    {
        scanf("%d%d",&n,&k);
        if(n>=k) 
        {
        printf("%d",n-k);
        return 0;   
        }
        printf("%d",bfs(n));
        return 0;
    }

    大老代码

    #include<cstdio>
    #include<iostream>
    using namespace std;
    int head=0,tail=1;
    int n,k;
    int f[199999];
    int d[9999999];
    int t[9999999];
    int bfs(int w)
    {
        d[1]=w;
        while(head<tail)
        {
            head++;int x;
            for(int i=1;i<=3;i++)
            {
    
                if(i==1) x=d[head]+1;
                if(i==2) x=d[head]-1;
                if(i==3) x=d[head]*2;
                if(f[x]==0&&x>=0&&x<=100000)
            {
                f[x]=1;
                d[++tail]=x;
            t[tail]=t[head]+1;if(d[tail]==k) return t[tail];
            }
            }
    
    
    
        }
        return 0;
    }
    int main()
    {
        scanf("%d%d",&n,&k);
        if(n>=k) 
        {
        printf("%d",n-k);
        return 0;    
        }
        printf("%d",bfs(n));
        return 0;
    }
  • 相关阅读:
    hdu 3613 Best Reward 扩展kmp
    hdu 4333 Revolving Digits 扩展kmp
    poj 1904 King's Quest 强连通
    hdu 3068 最长回文 manacher
    Codeforces Round #243 (Div. 2) C. Sereja and Swaps
    poj 3680 Intervals 费用流
    两个kmp hdu 2594 & hdu 2087
    hdu 3336 count the string
    Arcgis安装要素
    JS关闭窗口而不提示
  • 原文地址:https://www.cnblogs.com/wspl98765/p/6819898.html
Copyright © 2011-2022 走看看