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 ...
随机推荐
- UBUNTU 14.04 + CUDA 7.5 + CAFFE
这个也是困扰我很久的问题,之前用 http://www.cnblogs.com/platero/p/3993877.html 的安装方法,装了五六七八九十次,总是出问题. 后来找到了一种新的方法,一个 ...
- ios开发--苹果企业开发者账号
苹果企业开发者账号添加多个开发成(组)员的方法 如果你们公司有一个帐号,你是管理员,加入组员的办法如下: 第一步:进入Member Center页 第二步:点击people,会见到 第三步:点击Inv ...
- 120条Photoshop新手必看技巧
Photoshop越来越强大了!试图掌控它的全部特性是不现实的(更何况有那么多隐藏的功能!),那么我们不妨收藏一下大神们总结的这120个PS技巧,偶尔翻看一下,让自己的设计更强大更高效! 这120款技 ...
- TaskController.java-20160611
package main.java.com.zte.controller.system; import java.io.PrintWriter;import java.util.ArrayList;i ...
- SNMP SNMP协议
SNMP协议 一.什么是SNMP? SNMP是简单网络管理协议[Simple Network Management Protocol],由一组网络管理的标准组成,包含一个应用层协议(applicati ...
- VIM的配置文件(vimrc)在哪里?【Win7】
如果你使用VIM有一段时间的话,你会想要修改它的一些配置,例如默认显示行号.在哪改呢? 答案是:vimrc 那这个配置文件在哪呢? 打开你的vi,在命令模式下,输入[:version],会看到如下图所 ...
- Spring Injection with @Resource, @Autowired and @Inject
August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...
- MySQL之Join
参见MySQL(以5.1为例)中官方手册:MySQL官方手册-JOIN 假设有以下几个表 t1 id book 1 java 2 c++ 3 php t2 id author 2 zhang 3 wa ...
- dom4j API使用简介
dom4j API使用简介 功能简介 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极 ...
- HTML+CSS+JAVASCRIPT 总结
1. HTML 1: <!doctype html> 2: <!-- This is a test html for html, css, javascript --> 3: ...