HDU 2717

来源:互联网 发布:立体设计图软件 编辑:程序博客网 时间:2024/05/29 14:00
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;}


0 0
原创粉丝点击