HDU 2717

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8764    Accepted Submission(s): 2762

Problem 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

//此题是在一条水平线上追赶羊,分为三种情况 x-1 /x+1 /2*x   注意边界即可

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n,start,stop;
int p[210];
int vis[210];
struct Node
{
    int floor;
    int step;
};

int bfs(int flo)
{
    queue <Node> q;
    memset(vis,0,sizeof(vis));
    Node a;
    a.floor=flo,a.step=0;
    q.push(a);
    while(!q.empty())
    {
        Node b=q.front();
        q.pop();
        vis[b.floor]=1;
        if(b.floor==stop) return b.step;
        for(int i=0;i<2;i++)  //0 up 1 down
        {
            Node c=b;
            if(i==0)
                c.floor=c.floor+p[c.floor];
            else
                c.floor=c.floor-p[c.floor];

            if(!vis[c.floor]&&c.floor>=1&&c.floor<=n)
            {
                c.step++;
                q.push(c);
                vis[c.floor]=1;
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d%d",&start,&stop);
        for(int i=1;i<=n;i++)
            scanf("%d",&p[i]);
        printf("%d\n",bfs(start));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-17 20:02:38

HDU 2717的相关文章

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow Problem 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

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

HDU 2717 Catch That Cow(bfs)

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10575    Accepted Submission(s): 3303 Problem Description Farmer John has been informed of the location of a fugitive cow and want

杭电 HDU 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8999    Accepted Submission(s): 2837 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

HDU 2717 深搜第一题、

题意:求n到k的最小路径,  n有三种变法 n+1,n-1或者2*n: 贴个广搜的模版在这里把.... 总结一下:一般涉及到求最短路的话用深搜 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<queue> 5 #include<cstring> 6 using namespace std; 7 const int qq=1e5+10; 8 int v

hdu 2717 &amp;&amp; poj 3278 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9339    Accepted Submission(s): 2925 Problem Description Farmer John has been informed of the location of a fugitive cow and wants