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 ...
随机推荐
- C++运算符重载——重载二元运算符
1.重载二元操作符的方法 二元运算符又称为双目运算符,即需要2个操作数的运算符,例如 + - * / 等. 运算符重载可以分为3种方式:类的非静态成员函数.类的友元函数.普通函数. 例如有 2 个操作 ...
- mq_notify
NAME mq_notify - 通知进程可以接收一条消息 (REALTIME) SYNOPSIS #include <mqueue.h> int mq_notify(mqd_t mqde ...
- tomcat console
1.大家都知道,在Tomcat5及其以后的版本中,当启动tomcat之后,是看不到控制台中的manager应用的.Manager的应用还是很有好处的,可以直接在控制台上(类似于weblogic上的co ...
- 一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事
1.一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事
- Java学习笔记之:Java 内部类
一.介绍 内部类:存在与类中的类就是内部类,一般用于Android开发. 可以把内部类理解成一种继承关系 1.普通内部类 2.局部内部类 3.静态内部类 4.匿名内部类 二.笔记 1.普通内部类 /* ...
- Java:IO流之字节流InputStream、OutputStream详解
字节流: (抽象基类)InputStream类(读): (抽象基类)OutputStream类(写): InputStream: 构造方法摘要 InputStream() ...
- 测试用例生成工具ALLPAIRS(转)
ALLPAIRS是一个测试用例设计工具,用于Windows,但移植到了多种平台,以适应该脚本文件的一些小改动.它自动对所有实验技术进行设计,通过这个工具的方法可以在海量的数据组合中选择少量的数据生成测 ...
- Crypto API加密通信流程
应用程序使用Crypto API进行加密通信的一般步骤如下: 1,include wincrypt.h 2,调用CryptAcquireContext()获得某个CSP模块中的密钥容器(key con ...
- python中的 zip函数详解
python中zip()函数用法举例 定义:zip([iterable, ...]) zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple ...
- C#中的文件同步
How to: Synchronize Files by Using Managed Code FileSyncProvider Class File Synchronization Provider ...