搜索_Catch That Cow

source

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

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.

主要思路:BFS 可以做

#include
#include
#include
#define MAX 100001
using namespace std;
queue q;
bool visit[MAX];
int step[MAX];
bool bound(int num)
{
    if(num<0||num>100000)
        return true;
    return false;
}
int BFS(int st,int se)
{
    queue q;
    int t,temp;
    q.push(st);
    visit[st]=true;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        for(int i=0;i<3;++i)
        {
            if(i==0)
                temp=t+1;
            else if(i==1)
                temp=t-1;
            else
                temp=t*2;
            if(bound(temp))
                continue;
            if(!visit[temp])
            {
                step[temp]=step[t]+1;
                if(temp==se)
                    return step[temp];
                visit[temp]=true;
                q.push(temp);
            }
        }
    }
}
int main()
{
    int st,se;
    while(scanf("%d%d",&st,&se)!=EOF)
    {
        memset(visit,false,sizeof(visit));
        if(st>=se)
            printf("%d\n",st-se);
        else
        printf("%d\n",BFS(st,se));
    }
    return 0;
}
```

```
#include 
#include 
#include 
using namespace std;
const int N = 1000000;
int vis[N+10];
int n,k;
struct node
{
    int x,step;
};
int check(int x)
{
    if(x<0 || x>=N || vis[x])
        return 0;
    return 1;
}
int bfs(int x)
{
    int i;
    queue Q;
    node a,next;
    a.x = x;
    a.step = 0;
    vis[x] = 1;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.x == k)
            return a.step;
        next = a;
        next.step = a.step+1;
        for(int i=0;i<3;i++)
        {
            if(i==0)        next.x = a.x+1;
            else if(i==1)   next.x = a.x-1;
            else            next.x = a.x+a.x;
            if(check(next.x))
            {
                vis[next.x] = 1;
                Q.push(next);

            }
        }
    }
    return -1;
}
int main()
{
    int ans;
    while(~scanf("%d%d",&n,&k))
    {
        memset(vis,0,sizeof(vis));
        if(n>=k) printf("%d\n",n-k);
        else
        {
             ans = bfs(n);
            printf("%d\n",ans);
        }
    }
    return 0;
}

```

你可能感兴趣的:(搜索_Catch That Cow)