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 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.

求n到k需要多少步变化有(n+1,n-1,n*2)三种选择;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
#define N 100010

int m,n;
int vis[N];
struct node
{
    int x,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
};

int dfs()
{
    priority_queue<node>Q;
    memset(vis,0,sizeof(vis));
    node q,s;
    s.x=n;
    vis[s.x]=1;
    s.step=0;
    Q.push(s);
    int i;
    while(!Q.empty())
    {
        q=Q.top();
        Q.pop();
        if(q.x==m)
                return q.step;
        for(i=0;i<3;i++)
        {
            if(i==0)
                s.x=q.x+1;
            else if(i==1)
                s.x=q.x-1;
            else if(i==2)
                s.x=q.x*2;
            if(s.x<100001&&s.x>=0&&vis[s.x]==0)//vis[s.x]==0必须放到后面,-_-被运行错误错了好多次;
            {
                vis[s.x]=1;
                s.step=q.step+1;
                Q.push(s);
            }

        }
    }
    return -1;//要有返回值,我也不知道为什么;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(m==n)
        {
            printf("0\n");
            continue;
        }
        int ans;
        ans=dfs();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-26 17:30:47

Catch That Cow--POJ3278的相关文章

Catch That Cow POJ-3278 BFS

题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两倍的地方.求FJ能找到牛的最短时间. 思路 BFS.在每一个点有三种选择,前进,后退,或者传送.要注意的是,由于有后退的过程,所以可能会造成环,导致队列长度很长就直接MLE了.因此要用一个vis数组来控制不能选择已经去过的地方. 题解 1 #include <iostream> 2 #includ

【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

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

Catch That Cow DescriptionFarmer 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 l

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10

Catch That Cow

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 113   Accepted Submission(s) : 46 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to