poj 3278 Catch That Cow 广搜

hdu 2717 Catch That Cow,题目链接

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11466 Accepted Submission(s): 3551

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

Source

USACO 2007 Open Silver

Recommend

teddy | We have carefully selected several similar problems for you: 2102 1372 1240 1072 1180

题意:给你两个位置k和n,求出从k到n的最小步数。
从k有三种方式变化方式: k+1,k-1,k*2。

队列广搜,一个vis数组查看是否访问过,一个step数组记录步数。

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
#include<string.h>
#include<math.h>
#define maxn 100005
using namespace std;
int vis[maxn];//标记是否访问过;
int step[maxn];//记录步数;
queue <int > q ;
int serch(int k,int n)
{
    int flag=0,a,mark=0;
    memset(vis,0,sizeof(vis));//数组清零;
    memset(step,0,sizeof(step));//数组清零;
    q.push(k);  //将k入队;
    vis[k]=1;   //标记为1;
    while(!q.empty())   //当数组非空时;
    {
        a=q.front();    //将队列首位赋值给a;
        q.pop();        //删除队列首位;
        for(int i=1; i<=3; i++)
        {
            if(i==1)
                flag=a+1;
            else if(i==2)
                flag=a-1;
            else
                flag=a*2;
            if(flag>100000||flag<0)//越界判断;
                continue;
            if(!vis[flag])//如果标记为0;
            {
                step[flag]=step[a]+1;//当前步数等于上次步数+1;
                vis[flag]=1;//标记为1;
                q.push(flag); //入队;
                if(flag==n)//如果当前值为n;
                    return step[flag];//返回步数;
            }
        }
    }
    return 0;
    //如果是提交C++的话,要不要返回值都可以;
    //但如果是G++的话,必须要一个return 0;
    //这个是编译器的问题,具体也不是很清楚;
}
int main()
{
    int n,k;        //k追n;
    while(~scanf("%d%d",&k,&n))//在这里我写成了k追n;
    {
        if(k>n) //如果k>n,只能通过不断减一得到,也就是k-n步;
            printf("%d\n",k-n);
        else
            printf("%d\n",serch(k,n));
    }
    return 0;
}

之前关于那个return 0,wa了,好坑,换了编译器就A了。坑

T^T,刚刚问了远航学长关于那个C++提交,G++提交不同的问题,结果学长也不知道,我也是第一次遇到这种问题,好忧桑~~
不过学长也说没见过,他说只是在这个oj上遇到的是 double要用 %.f 好坑的OJ~~QAQ

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determine a race strategy which will get one of them across the finish line as fast as possible. Like everyone else, cows race bicycles in packs because that's the most efficient way to beat the wind. While travelling at x laps/minute (x is always an integer), the head of the pack expends x*x energy/minute while the rest of pack drafts behind him using only x energy/minute. Switching leaders requires no time though can only happen after an integer number of minutes. Of course, cows can drop out of the race at any time. The cows have entered a race D (1 <= D <= 100) laps long. Each cow has the same initial energy, E (1 <= E <= 100). What is the fastest possible finishing time? Only one cow has to cross the line. The finish time is an integer. Overshooting the line during some minute is no different than barely reaching it at the beginning of the next minute (though the cow must have the energy left to cycle the entire minute). N, D, and E are integers. Input A single line with three integers: N, E, and D Output A single line with the integer that is the fastest possible finishing time for the fastest possible cow. Output 0 if the cows are not strong enough to finish the race. Sample Input 3 30 20 Sample Output 7 Hint as shown in this chart: leader E pack total used this time---leader--speed--dist---minute 1------1-------5------5------25 2------1-------2------7-------4 3------2*------4------11-----16 4------2-------2------13------4 5------3*------3------16------9 6------3-------2------18------4 7------3-------2------20------4 * = leader switch Source USACO 2002 February

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值