ACM 之 B - Catch That cow

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

[hit : (5->17) 5-10-9-18-17 , 一共走了4步 。]

理解

John的牛跑了,他要用最快的方式抓到這頭牛,他可以選擇后退一步或者前進(jìn)一步甚至前進(jìn)當(dāng)前所在坐標(biāo)的二倍,找出最快的路徑幫他抓到牛。

代碼部分

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int con,x,y,f,b[200000];//數(shù)組開到這里是極限,再小就會(huì)數(shù)據(jù)溢出。
queue<int>a;
int main()
{
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        while(!a.empty())
        {
            a.pop();
        }
        memset(b,0,sizeof(b));
        a.push(-2);
        con=0;
        f=0;
        if(x==y) 
            {printf("0\n");continue;}
        a.push(x);
        b[x]=1;
        int i=1;
        while(f!=1)
        {
            if(a.front()==-2)
            {
                a.pop();
                con++;
                a.push(-2);
            }
            if((a.front()-1)==y||(a.front()+1)==y||(a.front()*2)==y)
                {f=1;break;}
            if(b[a.front()-1]==0&&a.front()>1&&a.front()<100001)
                {a.push(a.front()-1);b[a.front()-1]=1;}//b數(shù)組是存在標(biāo)記,表示出現(xiàn)過這個(gè)數(shù)了下次就不要再次運(yùn)算,可以提高程序效率。
            if(b[a.front()+1]==0&&a.front()>-1&&a.front()<99999)
                {a.push(a.front()+1);b[a.front()+1]=1;}
            if(b[a.front()*2]==0&&a.front()>0&&a.front()<50001)
                {a.push(a.front()*2);b[a.front()*2]=1;}
            a.pop();
            //cout<<i++<<endl;
        }
        printf("%d\n",con);
    }
    return 0;
}

意見反饋 || 任何建議

聯(lián)系我(新浪)
郵箱:qianlizhihao@gmail.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,217評(píng)論 0 23
  • 鬼腳七,名文德,前阿里高管,寫了兩本書。一本叫《沒事別隨便思考人生》,一本是今年才出版的《所有經(jīng)過的路都是必經(jīng)之路...
    山谷里的百合閱讀 1,030評(píng)論 0 0
  • 人世喧嚷,語聲熱浪,鼎沸的故事跌入其中,竟是靜默而微茫。想來有些難過,因?yàn)閻酆拗K究無可言說;然而又有竊自的歡喜...
    宛丘子閱讀 2,998評(píng)論 1 10
  • 孤獨(dú)有個(gè)秘密,但它從來不說 有人在孤獨(dú)中披荊斬棘 有人在歡樂中日漸消沉 孤獨(dú)的枷鎖是背負(fù)每個(gè)人的沉重 陽光來了,帶...
    白蓮花的復(fù)仇閱讀 248評(píng)論 0 0
  • 抓教研促質(zhì)量,一直以來是我校領(lǐng)導(dǎo)大力提倡的教學(xué)方針。
    火一樣的熱情閱讀 324評(píng)論 0 0

友情鏈接更多精彩內(nèi)容