poj3278Catch That Cow

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 88361   Accepted: 27679

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 - 1 or + 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

USACO 2007 Open Silver

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int que[200001];
int start=0,endd=0;
int time[200001]={0};
int vis[200001]={0};
int n,k;

int bfs(int n,int k){
    memset(que,0,sizeof(que));
    memset(time,0,sizeof(time));
    memset(vis,0,sizeof(vis));
    start=0;
    endd=0;
    que[endd++]=n;
    vis[n]=1;
    while(start<endd){
        int t=que[start];
        start++;
        for(int i=0;i<3;i++){
            int tt=t;
            if(i==0){
                tt+=1;
            }else if(i==1){
                tt-=1;
            }else if(i==2){
                tt*=2;
            }
            if(tt>100000||tt<0){
                continue;
            }
            if(!vis[tt]){
                vis[tt]=1;
                que[endd]=tt;
                time[tt]=time[t]+1;
                if(tt==k){
                    return time[tt];
                }
                endd++;
            }
        }
    }
}

int main()
{
    int ans;
    while(~scanf("%d %d",&n,&k)){
        if(n<k){
            ans=bfs(n,k);
        }else{
            ans=n-k;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-09 01:01:18

poj3278Catch That Cow的相关文章

POJ3278Catch That Cow(线性模型)(BFS)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49988   Accepted: 15679 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,00

POJ3278Catch That Cow(BFS+水题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 64176   Accepted: 20156 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,00

PoJ3278--Catch That Cow(Bfs)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61317   Accepted: 19155 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,00

poj3278--Catch That Cow(bfs)

  Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K       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

搜索专讲

搜索专讲 Tags:搜索 https://www.zybuluo.com/xzyxzy/note/1058215 前言 做一个专题肯定是要花点时间的 但是哇,搜索怎么这么多内容?!WTF?! 好吧慢慢刷,待四五月份左右出pdf或ppt的讲义吧 先把题目放上,大家愿意的和我一起做吧 题目 李老师给了一个包 广搜 [x] ?POJ1426-Find The Multiple https://vjudge.net/problem/POJ-1426 [x] POJ2251-Dungeon Master

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c

[USACO11FEB] Cow Line

https://www.luogu.org/problem/show?pid=3014 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line