hdu 2717 解题报告
来源:互联网 发布:返利网程序源码 编辑:程序博客网 时间:2024/06/16 09:03
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?
* 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
4HintThe 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 100005using 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做。。以后写好了代码再补充好了
恩。。已经挺晚的了,今天就这样吧~
0 0
- hdu 2717 解题报告
- HDU 3342 解题报告
- HDU 3336 解题报告
- HDU 3335 解题报告
- hdu 2516解题报告
- hdu 1004解题报告
- hdu 2139解题报告
- hdu 1019解题报告
- hdu 1064 解题报告
- HDU 1113 解题报告
- hdu 1068 解题报告
- HDU:2050解题报告
- hdu 4001解题报告
- hdu 1005解题报告
- HDU解题报告--1003
- HDU解题报告--1004
- HDU解题报告--1005
- HDU 1005 解题报告
- Android学习之Shared Preferences使用
- ubuntu 10.04 usb wifi 使用wpa_supplicant无法连网问题
- 修改才是写作的本质所在
- iOS开发-数据选择UIPickerView(案例二)
- 安卓 防止Bitmap产生OOM(OutOfMemoryError)错误
- hdu 2717 解题报告
- 管理上的小感悟
- iOS开发-数据选择UIPickerView(案例三)
- spring mvc中的@PathVariable[动态参数类似于spring中的通配符功能]
- 【图像处理】SFR算法详解2
- markdown使用haroopad导出PDF
- imagettftext(): any2eucjp(): invalid code in input string
- CodeForces 361D Levko and Array(二分+dp)
- 查找 - 《数据结构1000 + 大话数据结构》-练习题