【poj3278】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 - 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

题解

题意:给定两个整数n和k,通过 n+1或n-1 或n*2 这3种操作,使得n==k,输出最少的操作次数

bfs,找到的第一组就是最小步数

#include<iostream>
#include<cstdlib>
#include<queue>
#define N 1000000
using namespace std;
struct node
{
    int x,step;
};
queue <node> Q;
node first,now;
bool vis[N+10];
int n,k,ans;
int check(int x)
{
    if (x<0 || x>N ||vis[x]) return 0;
    return 1;
}
void extend(node in)
{
    node p = in;
    p.step ++;
    p.x = in.x*2;
    if (check(p.x)) vis[p.x] = true,Q.push(p);
    p.x = in.x+1;
    if (check(p.x)) vis[p.x] = true,Q.push(p);
    p.x = in.x-1;
    if (check(p.x)) vis[p.x] = true,Q.push(p);
}
int bfs()
{
    Q.push(first);
    while (!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if (now.x == k) return now.step;
        extend(now);
    }
}
int main()
{
    cin>>n>>k;
    first.x = n;
    cout<<bfs();
}
时间: 2024-10-27 08:11:08

【poj3278】Catch That Cow的相关文章

【BFS】Catch That Cow(POJ3278)

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 Jo

POJ3278,Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46459   Accepted: 14574 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

矩阵十题【十】 poj 3613 Cow Relays

题目链接:http://poj.org/problem?id=3613 题目大意: 输入N,T,S,E,N表示要走的边数,T表示一共有几条边,S表示开始的点,E表示结束的点 给出一张无向连通图,求S到E经过N条边的最短路. N (2 ≤ N ≤ 1,000,000) T (2 ≤ T ≤ 100) (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000) 1 ≤ lengthi  ≤ 1,000 题目主要的思想就是用矩阵的乘法模拟出Floyd进行运算,是个很好的题目. //k步最短路

poj3278(Catch That Cow)

题目大意: 一个农主寻找牛.给出农主的位置n和牛的位置k.农主可以通过n-1或者n+1或者n*2的步伐找牛,问至少多少步才能找到自己的牛. 解题思路: 简单的BFS.把农主的每一种可能的步伐通过BFS存到栈中,然后看最少多少步到达K坐标. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstri

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

【搜索】POJ3278: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 Jo

【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int v[50010]; 6 int n,m,d,l,ans; 7 int main() 8 { 9 scanf("%d%d%d%d",&n,&

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ? 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个 最远农场间的距离. Input ? 第1行:两个分开的整数N和M. ? 第2到M+1行:每行包括4个分开的内容,Fi,F2,L,D分别描述两个农场的编号,道路的长 度,F1到F2的方向N,E,S,W. Output ? 一个