POJ 3278 Catch That Cow-模拟

Catch That Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 101298 Accepted: 31669

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX + 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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]


转载一个逗比题意:

数学老师用你刚刚编写的程序验证了WNJXYK给出的回答,发现他全都是在口胡。老师很生气,后果很严重。于是,WNJXYK决定先跑为妙,他一下子跑出了很远觉得自己很安全就停下来休息了。

我们把世界抽象为一个数轴,老师一开始在点N(0 ≤ N ≤ 100,000),WNJXYK会一直在点K(0 ≤ K ≤ 100,000) 休息,老师每秒钟有两种操作:
1.走路:当前在X,下一秒可以在X+1或X-1
2.闪现:当前在X,下一秒在2*X

现在数学老师想知道,最早在什么时候才能抓住WNJXYK(到达他所在的位置)

分析:BFS模拟一下即可,注意一下不要数组越界;


AC Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1
const int maxn=200000+50;

struct node{
    int n,time;
};
int vis[maxn];

node walk(node a,int i){
    if(i==1) a.n++;
    else if(i==2) a.n*=2;
    else a.n--;
    a.time++;
    return a;
}

void bfs(int n,int k){
    if(k<=n){
        printf("%d\n",n-k);
        return;
    }
    memset(vis,0,sizeof vis);
    vis[n]=1;
    queue<node> q;
    node s; s.n=n; s.time=0;
    q.push(s);
    while(!q.empty()){
        s=q.front(); q.pop();
        if(s.n==k){
            printf("%d\n",s.time);
            return;
        }
        for(int i=1;i<=3;i++){
            node t=walk(s,i);
            if(t.n>maxn||t.n<0) continue;
            if(!vis[t.n]){
                q.push(t);
                vis[t.n]=1;
            }
        }
    }
}

int main() {
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        bfs(n,k);
    }
    return 0;
 }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值