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;
    }
  • 相关阅读:
    梦断代码阅读笔记02
    第三周
    第二周
    《大道至简》阅读笔记
    暑假第一周
    hdu-1237简单计算器(栈的运用)
    玩骰子(概率,暴力)
    hdu-5568SUM (dp)
    codeforce-600C. Make Palindrome(贪心)
    codeforce-601A. The Two Routes(最短路)
  • 原文地址:https://www.cnblogs.com/wspl98765/p/6819898.html
Copyright © 2011-2022 走看看