您的位置:首页 > 其它

Catch That Cow && A strange lift

2013-07-23 08:27 127 查看

Catch That Cow

Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 11

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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


#include<stdio.h> 

#include<queue>

#include<string.h> 

using namespace std;

typedef struct Node

{int x;

 int sum;

 }Node;

 int m,n;

 int flag[200000];

 

 int BFS()

 {Node p,q;

  queue<Node> qu;

  p.x=m;

  p.sum=0;

  qu.push(p);

  while(!qu.empty())

  { p=qu.front();

    qu.pop();

    if(m==n) return 0;

    if(p.x==n) return p.sum;

    p.sum++;

    q=p;

    q.x=q.x*2;

    if(q.x>=0&&q.x<=100000&&flag[q.x]){flag[q.x]=0;

       qu.push(q);}

    q=p;

    q.x--;

    if(q.x>=0&&q.x<=100000&&flag[q.x]) {flag[q.x]=0;qu.push(q);}

    q=p;

    q.x++;

    if(q.x>=0&&q.x<=100000&&flag[q.x])   { flag[q.x]=0; qu.push(q);}

  

    }

  }

 

 main()

 {int a;

 while(scanf("%d%d",&m,&n)!=EOF)

  {memset(flag,1,sizeof(flag));

  a=BFS();

  printf("%d\n",a);} 

  return 0;
  } 

A strange lift

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 54   Accepted Submission(s) : 14

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki
floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding
with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as
you know ,the -2 th floor isn't exist.

Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?

Input

The input consists of several test cases.,Each test case contains two lines.

The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.

A single 0 indicate the end of the input.

Output

For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".

Sample Input

5 1 5
3 3 1 2 5
0


#include <stdio.h>

#include <iostream>

#include <queue>

using namespace std;

int n,a,b;

bool map[205],flag;

int c[205];

struct node

{

    int x;

    int step;

}mp,n1,n2;

int bfs()

{

    queue<node>q;

    mp.x=a;

    mp.step=0;

    q.push(mp);

    map[mp.x]=true;

    while(!q.empty())

    {

        mp=q.front();

        q.pop();

        if(mp.x==b)         {flag=true; return mp.step;}

        n1.x=mp.x+c[mp.x];

        n2.x=mp.x-c[mp.x];

        if(n1.x>0&&n1.x<=b&&!map[n1.x])

        {

            n1.step=mp.step+1;

            map[n1.x]=true;

            q.push(n1);

        }

        if(n2.x>0&&n2.x<=b&&!map[n2.x])

        {

            n2.step=mp.step+1;

            map[n2.x]=true;

            q.push(n2);

        }

    }

}

int main()

{

    int i;

    while(scanf("%d",&n))

    {

        if(n==0)  {break;}

        scanf("%d%d",&a,&b);

        for(i=1;i<=n;i++)

        {

            scanf("%d",&c[i]);

            map[i]=false;

        }

        flag=false;

        bfs();

        if(flag)

        {

            printf("%d\n",mp.step);

        }

        else

        {

            printf("-1\n");

        }

    }

    return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: