POJ - 3278 Catch That Cow

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 90830   Accepted: 28508

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 - 1 or + 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.

Source

題意:農夫所在位置爲N,0≤N≤100000,奶牛所在位置爲K,0≤K≤100000,農夫和奶牛在同一條直線上,農夫每次移動可以向前走一步,或者退後一步,或者傳送到當前座標的2倍位置,比如從5到10,一步完成。奶牛並不知道農夫在抓它,所以一直在原地不動,問農夫最短可以用多少步抓到奶牛。
分析:一道BFS的題,但是DP也可以解,每次BFS都進行三次操作,前進一步,後退一步,傳送到二倍位置,如果沒有訪問過,則標記訪問,步數加一,直到找到解。因爲農夫後退只能一步一步退,所以用DP的時候先從農夫的初始位置向前掃描,每移動一次步數加一直到到0位置。
for(i=start;i>0;i--)
    	dp[i-1]=dp[i]+1;
再從初始位置向後掃描,後位置(dp[i])步數爲前一位置步數+1(dp[i-1]+1),再計算從二分之一當前座標傳送到目前位置的步數,取最小值,注意細節處理。當i爲奇數時dp[i]=min(dp[i],dp[i/2+1]+2);例如5需要3->6->5,一共兩步操作,當i爲偶數時dp[i]=min(dp[i],dp[i/2]+1);掃描完成後dp[奶牛初始位置]就爲最終答案。
經過測試DP和BFS內存使用不相上下,但DP4ms就能AC,BFS需要32ms。
細節:第一次DP數組只開了100000,一直WA,後來發現是數組不夠大,所以遇到這種類型的還是把數組多開幾個單位比較保險。
/*DP*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAX = 1e5+5;
int start,target;
int now ,pos;
int dp[MAX];
int i;
int main()
{
    cin >> start >> target;
    for(i=start;i>0;i--)
        dp[i-1]=dp[i]+1;
    for(i=start+1;i<=target;i++)
    {
        dp[i]=dp[i-1]+1;
        if (i%2==1) dp[i]=min(dp[i],dp[i/2+1]+2);
        else
            dp[i]=min(dp[i],dp[i/2]+1);
    }
    cout << dp[target] <<endl;
    return 0;
}
/*BFS*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAX=1E5+10;
static bool visit[MAX]={false};
static int step[MAX]={0};
int now,position;
int first,finally;
queue <int> que;
int BFS(int n)
{
    que.push(n);
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        for(int i=0;i<3;i++)
        {
            if (i==0)   position=now-1;
            else if(i==1) position=now+1;
            else position=2*now;//now 當前位置 position 經過一步移動後的位置
            if(visit[position]==false&&position>=0&&position<=MAX)
            {
                visit[position]=true;
                step[position]=step[now]+1;
                que.push(position);
            }
            if(position==finally)
                return step[position];
        }
    }
}
int main()
{
    cin >> first >> finally;
    while(!que.empty()) que.pop();
    memset(visit,false,sizeof(visit));
    memset(step,0,sizeof(step));
    if (first>=finally) cout << first-finally <<endl;
    else
        cout << BFS(first) << endl;
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章