Catch That Cow

Catch That Cow

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
找奶牛可以说是经典广搜题了,这次没有用“正难则反”思想,是“堂堂正正”广搜。RE了7,8发
先上RE代码

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<queue>
#define pii pair<int,int>
using namespace std;
const int N=100005;
bool vis[N];
int n,m;
int bfs(pii s){
    queue<pii>q;
    q.push(s);
    vis[s.first]=1;
    while(!q.empty()){
        pii nowpos=q.front();q.pop();
        if(nowpos.first==m)return nowpos.second;
        if(nowpos.first>m&&!vis[nowpos.first-1]&&nowpos.first<N){
                q.push(make_pair(nowpos.first-1,nowpos.second+1));
                vis[nowpos.first-1]=1;

        }
        else if(nowpos.first>=0){
            if(!vis[nowpos.first*2]&&2*nowpos.first<N&&nowpos.first)q.push(make_pair(nowpos.first*2,nowpos.second+1)),vis[nowpos.first*2]=1;
            if(!vis[nowpos.first+1]&&nowpos.first+1<N)q.push(make_pair(nowpos.first+1,nowpos.second+1)),vis[nowpos.first+1]=1;
            if(!vis[nowpos.first-1]&&nowpos.first-1>=0)q.push(make_pair(nowpos.first-1,nowpos.second+1)),vis[nowpos.first-1]=1;
        }
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m){
        memset(vis,0,sizeof(vis));
        cout<<bfs(make_pair(n,0))<<endl;
    }
    return 0;
}

下面是Ac代码

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<queue>
#define pii pair<int,int>
using namespace std;
const int N=100005;
bool vis[N];
int n,m;
int bfs(pii s){
    queue<pii>q;
    q.push(s);
    vis[s.first]=1;
    while(!q.empty()){
        pii nowpos=q.front();q.pop();
        if(nowpos.first==m)return nowpos.second;
        if(nowpos.first<N&&nowpos.first>m&&!vis[nowpos.first-1]){
                q.push(make_pair(nowpos.first-1,nowpos.second+1));
                vis[nowpos.first-1]=1;

        }
        else if(nowpos.first>=0){
            if(2*nowpos.first<N&&!vis[nowpos.first*2]&&nowpos.first)q.push(make_pair(nowpos.first*2,nowpos.second+1)),vis[nowpos.first*2]=1;
            if(nowpos.first+1<N&&!vis[nowpos.first+1])q.push(make_pair(nowpos.first+1,nowpos.second+1)),vis[nowpos.first+1]=1;
            if(nowpos.first-1>=0&&!vis[nowpos.first-1])q.push(make_pair(nowpos.first-1,nowpos.second+1)),vis[nowpos.first-1]=1;
        }
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m){
        memset(vis,0,sizeof(vis));
        cout<<bfs(make_pair(n,0))<<endl;
    }
    return 0;
}

         眼尖的小伙伴一定发现在查表时的数值

一定要先判边界

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值