C_Catch That Cow

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.

代码

//未简化版,将+1,-1,*2三种情况分开写的
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=100100;
typedef long long LL;
typedef pair<int,int> p;

int n,k;
int d[MAXN];//到达当前点的最短时间
bool flag[MAXN];//标记该点有没有走过,节省时间

int bfs(int n,int k)
{
    if(n==k) return 0;//本来人和牛就在一起的话,当然一分钟都不需要,直接抓住
    queue<int> q;
    q.push(n);
    flag[n]=1;
    while(q.size())
    {
        int qq=q.front();
        q.pop();
        if(qq+1<MAXN&&!flag[qq+1])//如果当前点在范围内并且没走过,就走一走
        {
            flag[qq+1]=1;
            d[qq+1]=d[qq]+1;
            if(qq+1==k) return d[qq+1];//如果当前点就是牛的位置,返回走到当前点的最短时间
            q.push(qq+1);
        }
        if(qq-1>=0&&!flag[qq-1])
        {
            flag[qq-1]=1;
            d[qq-1]=d[qq]+1;
            if(qq-1==k) return d[qq-1];
            q.push(qq-1);
        }
        if(qq*2<MAXN&&!flag[qq*2])
        {
            flag[qq*2]=1;
            d[qq*2]=d[qq]+1;
            if(qq*2==k) return d[qq*2];
            q.push(qq*2);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    memset(d,0,sizeof(d));
    memset(flag,0,sizeof(flag));
    cout<<bfs(n,k)<<endl;
    return 0;
}

——————————-我是萌哒哒的分割线————————————–

//简化版,将三种情况用一个for循环来实现
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=100100;
typedef long long LL;
typedef pair<int,int> p;

int n,k;
int d[MAXN];
bool flag[MAXN];
int a[3][2]={{1,1},{1,-1},{2,0}};

int bfs(int n,int k)
{
    if(n==k) return 0;
    queue<int> q;
    q.push(n);
    flag[n]=1;
    while(q.size())
    {
        int qq=q.front();
        q.pop();
        for(int i=0;i<3;i++)
        {
            if(qq*a[i][0]+a[i][1]>=0&&qq*a[i][0]+a[i][1]<MAXN&&!flag[qq*a[i][0]+a[i][1]])//看不懂的话就把a数组元素带进去
            {                    //可以发现这表示的就是那三种情况
                flag[qq*a[i][0]+a[i][1]]=1;
                d[qq*a[i][0]+a[i][1]]=d[qq]+1;
                if(qq*a[i][0]+a[i][1]==k) return d[qq*a[i][0]+a[i][1]];
                q.push(qq*a[i][0]+a[i][1]);
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    memset(d,0,sizeof(d));
    memset(flag,0,sizeof(flag));
    cout<<bfs(n,k)<<endl;
    return 0;
}

你可能感兴趣的:(C_Catch That Cow)