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 ...
随机推荐
- Spring框架学习之第5节
request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...
- Ubuntu LAMP搭建
为了数据库课程设计,只好自己搭一个数据库系统,采用LAMP方式. 一.安装 1.安装Apache sudo apt-get install apache2 Apache在安装期间会新建一个目录:/va ...
- Merry Christmas 2015
祝大家圣诞快乐! 昨天下班在电梯里遇见HR大BOSS,她说公司今天上午有2200个员工要带小孩子来参加Children's Holidy Party...我问了句,那是不是有免费早餐和午餐啊,她说 & ...
- 3、Object对象的两大方法(hashCode-equals)总结
Object类是所有java类的父类. 用户定义了如下一个Person类 public class Person{} 在类定义中并没有明确继承Object类,但是编译器会自动的完成这个过程. 既然所有 ...
- java Comparable和Comaprator的对比
Comparable使一个class具备不同实例间进行比较的行为.这些对象的集合,可作为Collections.sort或Arrays.sort的参数 Comparator可以看成一种算法的实现,将算 ...
- 习惯&感恩
A不喜欢吃鸡蛋,都给了B吃,刚开始B很感谢,久而久之便习惯了.习惯了,便理所当然了. 后来有一天,A将鸡蛋给了C,B就不爽了.她忘记了这个鸡蛋本来就是A的,A想给谁都可以. 为此,她们大吵一架,从此绝 ...
- js中Number
var numberObject=new Number(1333);numberObject.valueOf(); 1333 var numberObject=new Number(1333);num ...
- asp数据链接
asp页面的中的数据库连接要进行唯一名称处理,不然页面中多个连接使用时,会出现连接莫名关闭.数据不能写入,但是页面也没报错.asp可以将有数据库连接的地方,提取成单独的函数.在每个方法单独命名数据库连 ...
- TCSRM5961000
一直没想到怎么去重 看了眼别人的代码...so easy啊 同余啊 唉..脑子被僵尸吃掉了 难得1000出个简单的 #include <iostream> #include<cstd ...
- JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告
这几天学习 JSON - SuperObject, 非常幸运地得到了其作者 Henri Gourvest 大师的同步指点! (Henri 大师也是 DSPack 和 GDI+ 头文件的作者; 大师是法 ...