hdu 2717 解题报告

4 篇文章 0 订阅

hdu 2717

Catch That Cow

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


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.


大致题意:有个人要追一头牛,给出人和牛的坐标,人的坐标可以加1,或者减1,或者使自己的坐标边为原来的两倍。问要使人追上牛(即坐标相等),最少需要几次

解题思路:这题一下子看出了智商的差距。。。我自己做的时候,可能也是因为做寒假作业的时候上面有提示用搜索,没多想就想直接bfs暴力。。后来有个同学写了个代码叫我帮忙debug才发现他的做法简单很多orz。。。不知道怎么称呼。。感觉像是动态规划(能力有限诸多不懂见谅。。)

搜索没什么多说的,就是很暴力的做法==具体看代码

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 100005
using namespace std;

long long a[MAXN],time[MAXN],vis[MAXN],cur=0,next=1,t;

void bfs(long long s, long long e)
{
    a[cur]=s;
    time[s]=0;
    vis[s]=1;
    while(cur<next)
    {
        for(int i=0; i<3; i++)
        {
            if(i==0)t=a[cur]+1;
            else if(i==1)t=a[cur]-1;
            else if(i==2)t=a[cur]*2;
            if(t<0||t>100000||vis[t])
            {
                continue;
            }
            else
            {
                vis[t]=1;
                a[next]=t;
                time[t]=time[a[cur]]+1;
                next++;
            }
            if(t==e){printf("%lld\n",time[t]);return;}
        }
        cur++;
    }
}

int main()
{
    long long N,K;
    scanf("%lld %lld",&N,&K);
    memset(vis,0,sizeof(vis));
    if(N>=K)printf("%lld\n",N-K);//注意这里!!当时没发现有这种情况。。看了大神的解题报告才发现orz。。
    else bfs(N,K);
    return 0;
}


接下来的就是简单的做法了orz。。智商低没想到这么做啊
思路就是相当于从牛的坐标开始倒着思考,走这一步的上一个状态有三种情况,要么就是这个坐标的一半,要么是这个坐标加1,要么是这个坐标减1,要使次数最少,我们就要尽量选择最优的走法,显然能从当前坐标的一半走过来的话就是最优的,至少是一样的。。。哎。。解释不是很清楚。。。注释在代码里好了。。,代码也是很简洁易懂的(再次膜拜orz。。)

#include<iostream>
#include <algorithm>
#include<cmath>
using namespace std;
long long m;
long long fun(long long n)
{
  if(n<=m) return m-n;
  if(n==1) return 1;
  if(n%2==0)//若当前坐标是偶数
  {
    if(n/2>=m) return fun(n/2)+1;//如果可以从坐标的一半走过来,必然是最优的
    else return min(fun(n/2)+1,n-m);//如果坐标的一半小于人的坐标,选择上一步少的走
  }
  else//奇数同理
  {
    if((n-1)/2>=m) return min(fun((n-1)/2),fun((n+1)/2))+2;
    else return min(min(fun((n-1)/2)+2,fun((n+1)/2)+2),n-m);//这里括号比较多,注意一下。。当时的错就是不小心少了个括号。。
  }
}
int main()
{
  long long n;
  cin>>m>>n;
  cout<<fun(n)<<endl;
  return 0;
}

题后反思:首先,就是输入数据的大小关系有的时候不能确定。。这个错过好多次了==以后要多注意。。
其次就是做完了一题不要就这么算了。。说不定还有更简单的做法呢。。

然后就是那个bfs,还可以用stl的queue做。。以后写好了代码再补充好了
恩。。已经挺晚的了,今天就这样吧~









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值