• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj3278

            Catch That Cow

            Time Limit: 2000MS Memory Limit: 65536K
            Total Submissions: 27595 Accepted: 8495

            Description

            Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

            * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
            * Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

            If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

            Input

            Line 1: Two space-separated integers: N and K

            Output

            Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

            Sample Input

            5 17

            Sample Output

            4

            Hint

            The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

            挺簡單的廣搜,不知道哪里錯了,wa了近20遍,悲劇,可能是邊界沒處理好
             1#include<stdio.h>
             2#include<string.h>
             3#include<math.h>
             4#define inf 100000
             5#define MAX 1000000
             6int n,k,ans;
             7long long q[MAX+1],number[inf+1];
             8short flag[inf+1];
             9int bfs()
            10{
            11    int head,tail;
            12    long long now,now1;
            13    head=0;
            14    tail=1;
            15    q[tail]=n;
            16    flag[n]=1;
            17    number[n]=1;
            18    while(head<tail)
            19    {
            20        head++;
            21        now=q[head];
            22        if (now==k)
            23        {
            24            return number[now]-1;
            25        }

            26        now1=now-1;
            27        if (now1>=0&&now1<=inf&&!flag[now1])
            28        {
            29            tail++;
            30            q[tail]=now1;
            31            flag[now1]=1;
            32            number[now1]=number[now]+1;
            33        }

            34        now1=now+1;
            35        if (now1>=0&&now1<=inf&&!flag[now1])
            36        {
            37            tail++;
            38            q[tail]=now1;
            39            flag[now1]=1;
            40            number[now1]=number[now]+1;
            41        }

            42        now1=now*2;
            43        if (now1>=0&&now1<=inf&&!flag[now1])
            44        {
            45            tail++;
            46            q[tail]=now1;
            47            flag[now1]=1;
            48            number[now1]=number[now]+1;
            49        }

            50    }

            51}

            52int main()
            53{
            54    while (scanf("%d%d",&n,&k)!=EOF)
            55    {
            56        if(n==k)
            57        {
            58            printf("0\n");
            59            continue;
            60        }

            61        if (k==0)
            62        {
            63            printf("%d\n",n);
            64            continue;
            65        }

            66        memset(q,0,sizeof(q));
            67        memset(number,0,sizeof(number));
            68        memset(flag,0,sizeof(flag));
            69        ans=bfs();
            70        printf("%d\n",ans);
            71    }

            72    return 0;
            73}

            74
            75


            posted on 2012-03-20 19:44 jh818012 閱讀(171) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當(dāng)于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點擊標題查看
            • --王私江
            亚洲av日韩精品久久久久久a | 国产99精品久久| 久久精品夜夜夜夜夜久久| 国产aⅴ激情无码久久| 人人妻久久人人澡人人爽人人精品| 亚洲国产成人精品女人久久久 | 办公室久久精品| 一级做a爰片久久毛片看看| 久久精品国产99国产精品亚洲| 久久精品国产亚洲AV香蕉| 久久99国产精品二区不卡| 蜜桃麻豆www久久国产精品| 99精品国产综合久久久久五月天| 免费国产99久久久香蕉| 亚洲精品tv久久久久| 精品久久久久久无码中文字幕一区| 亚洲国产精久久久久久久| 久久午夜福利无码1000合集| 久久精品国产99国产电影网| 性做久久久久久久久浪潮| 人人狠狠综合久久亚洲婷婷| 亚洲欧美成人久久综合中文网| 99久久成人国产精品免费| 无码人妻久久一区二区三区蜜桃 | 一级做a爰片久久毛片免费陪| 久久久久高潮毛片免费全部播放 | 一本久久知道综合久久| 久久99精品九九九久久婷婷| 伊人久久大香线焦AV综合影院 | 无码AV波多野结衣久久| 久久艹国产| 国产精品欧美久久久久无广告 | 国产99久久久国产精品小说| 国产成人无码精品久久久久免费| 亚洲va久久久噜噜噜久久狠狠| 香蕉久久久久久狠狠色| 久久婷婷五月综合97色直播| 精品国产婷婷久久久| 久久免费高清视频| 久久96国产精品久久久| 久久91精品久久91综合|