关闭

poj 3278 Catch That Cow

标签: POJDFS
270人阅读 评论(0) 收藏 举报
分类:
Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 58434   Accepted: 18161

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.

思路:
对于任意起点,只有三种操作,即*2,+1,-1;每次操作所得值若小于100000且未访问过即入队列,当该值等于终点即求出最少步数。
                                              

代码:
#include<iostream>
#include<queue>
using namespace std;
int dis[100001],vis[100001];//dis[]步数,vis[]是否访问

int main()
{
    int n,k;
    int l;
    cin>>n>>k;
    queue<int> q;
    q.push(n);
    dis[n]=0;
    while(!q.empty())
    {
        l=q.front();
        q.pop();
        vis[l]=1;
        if(l==k)
            break;
        if( l-1>=0 && vis[l-1]==0)
        {
            q.push(l-1);
            vis[l-1] = 1;
            dis[l-1] = dis[l]+1;
        }
        if( l+1<=100000 && vis[l+1]==0)
        {
            q.push(l+1);
            vis[l+1] = 1;
            dis[l+1] = dis[l]+1;
        }
        if( l*2<=100000 && vis[l*2]==0)
        {
            q.push(l*2);
            vis[l*2]=1;
            dis[l*2]=dis[l]+1;
        }
    }
    cout<<dis[l]<<endl;
    return 0;
}



还有更快的:
memory   980K  time 16Ms

#include <iostream>   
#include <cstring>   
#include <cmath>   
#include <cstdlib>   
#include <algorithm>   
#include <string>   
#include <cstdio>   
#include <climits>   
using namespace std;   
bool data[100005] = {0};   
int que[100005] = {0};   
int tnum[100005] = {0};   
void bfs(int n, int k)   
{   
   int front = 0, rear = 0;   
    que[0] = n;   
    data[n] = true;   
    tnum[0] = 0;   
    rear++;   
       
    while (front != rear)   
    {   
        int t = que[front];   
        int tn = tnum[front];   
        if (t == k)   
        {   
            printf("%d\n", tn);   
            return;   
        }   
           
        if (t > 0 && !data[t-1])   
        {   
            data[t-1] = true;   
            que[rear] = t-1;   
            tnum[rear] = tn+1;   
            rear++;   
        }   
           
        if (t < 100000 && !data[t+1])   
        {   
            data[t+1] = true;   
            que[rear] = t+1;   
            tnum[rear] = tn+1;   
            rear++;   
        }   
        if (t <= 50000 && !data[t*2])   
        {   
            data[t*2] = true;   
            que[rear] = t*2;   
            tnum[rear] = tn+1;   
            rear++;   
        }   
        front++;   
    }   
    printf("0\n");   
}   
       
int main()   
{   
    int n, k;   
    scanf("%d%d", &n, &k);   
    memset(data, 0, sizeof(data));   
    bfs(n, k);   
    return 0;   
} 



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Catch That Cow 一道入门搜索题

Catch That Cow Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...
  • yangjiaronga
  • yangjiaronga
  • 2014-08-04 11:10
  • 2004

[kuangbin带你飞]专题一 简单搜索-C - Catch That Cow

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
  • sb_Ihateyou
  • sb_Ihateyou
  • 2017-04-13 19:40
  • 2175

Fliptile POJ3279 二进制压缩枚举 解题报告

D - Fliptile Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitS...
  • wr132
  • wr132
  • 2015-04-24 19:27
  • 1284

POJ3278 Catch That Cow题解 广度优先搜索+限定解空间

题目大意:直线上的两个坐标N和K,其中从N出发到达K,可以变换为+1、-1或者*2,每变换一次坐标,需要花费一分钟,求最短时间到达K。 分析:由于要求最优解,我们首先考虑使用BFS,相当于每次入队要...
  • LamdaSniper
  • LamdaSniper
  • 2017-11-21 09:40
  • 62

poj 3278 Catch that cow(BFS 广搜)

越来越觉得理解过程和代码实现绝对是两个层次的东西了。。上次并查集都啃了很久。。 广度优先搜索(BFS),概念很简答明了,媛姐讲时“分身”的说法很形象。 在方格迷宫中,怎样在最短时间内找到出口,要走...
  • zxc701
  • zxc701
  • 2011-08-23 10:15
  • 696

POJ-3278-Catch That Cow(BFS)

C - Catch That Cow Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su...
  • qq_32680617
  • qq_32680617
  • 2016-06-06 18:42
  • 145

POJ - 3278 Catch That Cow

使用queue进行bfs搜索
  • sinat_29278271
  • sinat_29278271
  • 2016-02-24 20:25
  • 187

POJ 3278 Catch That Cow(广搜BFS)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 81628  ...
  • xiangchengshishuiguo
  • xiangchengshishuiguo
  • 2017-01-11 18:43
  • 121

POJ - 3278----Catch That Cow

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
  • m765885195t
  • m765885195t
  • 2017-01-30 19:25
  • 329

Poj3278 BFS Catch That Cow

Catch That CowDescriptionFarmer John has been informed of the location of a fugitive cow and wants t...
  • AC_jie
  • AC_jie
  • 2017-05-02 20:41
  • 115
    个人资料
    • 访问:37540次
    • 积分:1013
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:9篇
    • 译文:0篇
    • 评论:5条
    最新评论