zoukankan      html  css  js  c++  java
  • C

    题目大意
    农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在NN (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛。

    也是非常非常水的题目,求最快当然是用广搜...

    //////////////////////////////////////////////////////////////

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;

    #define maxn 100001

    int v[maxn];//标记是否走过,防止重走

    int BFS(int s, int e)
    {
        queue<int> Q;

        Q.push(s);
        v[s] = 1;

        while(Q.size())
        {
            s = Q.front();
            Q.pop();

            if(s == e)
                return v[s]-1;

            for(int i=0; i<3; i++)
            {
                int q=s;

                if(i == 0)
                    q--;
                else if(i == 1)
                    q++;
                else
                    q *= 2;

                if(q>=0 && q <=maxn && v[q] == 0)
                {
                    Q.push(q);
                    v[q] = v[s]+1;
                }
            }
        }

        return -1;
    }

    int main()
    {
        int s, e;

        while(scanf("%d%d", &s, &e) != EOF)
        {
            memset(v, 0sizeof(v));

            int ans = BFS(s, e);

            printf("%d ", ans);
        }

        return 0;

    } 

  • 相关阅读:
    hdu 5059 判断数字表示方式以及范围合法(int型之内)
    hdu1024 最大m子序列和
    POJ 1573 Robot Motion 模拟 难度:0
    POJ 2632 Crashing Robots 模拟 难度:0
    POJ 1068 Parencodings 模拟 难度:0
    ncs安装及初次运行
    POJ 1328 Radar Installation 贪心 难度:1
    POJ 3094 Quicksum 难度:0
    django.test.client 使用随记
    POJ 2255 Tree Recovery 树的遍历,分治 难度:0
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4648084.html
Copyright © 2011-2022 走看看