HUD 2717 Catch That Cow
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12798 Accepted Submission(s): 3950
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
解析:简单BFS。易知农夫走的范围不会超过2*k,超过2*k就得不到最小值。
```
#include
#include
const int MAXN = 100000+5;
int dis[2MAXN];
int q[2MAXN];
int bfs(int n, int k)
{
memset(dis, -1, sizeof dis);
dis[n] = 0;
int l = 0, r = 0;
q[r++] = n;
while(l < r){
int h = q[l++];
if(h == k)
return dis[k];
int tmp = h-1;
if(tmp >= 0 && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
tmp = h+1;
if(tmp <= 2k && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
tmp = h2;
if(tmp <= 2*k && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
}
}
int main()
{
int n, k;
while(~scanf("%d%d", &n, &k)){
int res = bfs(n, k);
printf("%d\n", res);
}
return 0;
}
HUD 2717 Catch That Cow的更多相关文章
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Hdoj 2717.Catch That Cow 题解
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- NIM博弈的必胜取法
#include<stdio.h> ; int a[Max]; int main() { int i,n,ans; int tmp; while(scanf("%d", ...
- 2011 ACM-ICPC 成都赛区解题报告(转)
2011 ACM-ICPC 成都赛区解题报告 首先对F题出了陈题表示万分抱歉,我们都没注意到在2009哈尔滨赛区曾出过一模一样的题.其他的话,这套题还是非常不错的,除C之外的9道题都有队伍AC,最终冠 ...
- eclipse工程设置的问题
- 针对安卓java入门:方法的使用
[修饰符]返回值类型 方法名 (形式参数表){ 程序代码; return 返回值; } 例子: public class Test { //方法一 public static void printIn ...
- Intellij编译时报“java: System Java Compiler was not found in classpath” 解决办法
Intellij编译时报“java: System Java Compiler was not found in classpath” 解决方法: Project Settings > Comp ...
- 文件夹属性中只有"常规"解决办法
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shellex\PropertySheetHandlers] [HK ...
- C# 文件递归
C# 文件递归 Directory.GetDirectories: 获取指定目录下的文件夹,不包括子目录: Directory.GetFiles:获取指定文件夹下的文件,不包括子目录: 1.获取所 ...
- GitHub 教程 in Ubuntu
Follow these steps to configure github if you are the first time to use Github 1. Sign up a username ...
- Android 获取最近应用的缩略图
最近有项需求是获取应用的缩略,用于在动画时显示.因此就对此块知识简要了解了一下. 在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 新视频增加后需要SDCard重新扫描才能给新增 ...
- Python得到前面12个月的数据,Python得到现在时间 前一年的数据,
#Python 实现得到现在时间12个月前的每个月 # 假设现在的时间是2016年9月25日 #得到现在的时间 得到now等于2016年9月25日 now = datetime.datetime.no ...