POJ 3278 Catch That Cow(赶牛行动)

Time Limit: 1000MS    Memory Limit: 65536K

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 - 1 or + 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?

农夫John被告知有奶牛企图逃跑欲火速擒回。他的初始位置为某条线的N ( ≤ N ≤ ,)点,且奶牛在同一条线上的点K ( ≤ K ≤ ,)。农夫John有两种移动方式:要么走路,要么传送。

* 走路: FJ可以从任意的点X用一分钟移动到 X -  或 X +
* 传送: FJ可以从任意的点X用一分钟传送到2 × X 如果牛完全不动,农夫John需要多少时间才能追回它们?

CN

Input - 输入

Line 1: Two space-separated integers: N and K

1行:两个由空格分隔的整数:N和K

CN

Output - 输出

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

1行:最短时间,表示农夫John需要多少分钟追回逃跑的奶牛。

CN

Sample Input - 输入样例

5 17

Sample Output - 输出样例

4

题解

  判断好条件,开辟两倍的空间(*2),直接SPFA(BFS)。

代码 C++

 #include <cstdio>
#include <cstring>
#include <queue>
int data[];
int main(){
int n, k, now, nxt, i, j;
scanf("%d%d", &n, &k);
k <<= ;
memset(data, 0x7F, sizeof data); data[n] = ;
std::queue<int>q; q.push(n);
while (!q.empty()){
now = q.front(); q.pop();
j = now == ? : ;
for (i = ; i < j; ++i){
switch (i){
case :nxt = now + ; break;
case :nxt = now << ; break;
case :nxt = now - ; break;
}
if (i< && nxt > k) continue;
if (data[nxt] > data[now] + ){
data[nxt] = data[now] + ; q.push(nxt);
}
}
}
printf("%d\n", data[k >> ]);
return ;
}

最新文章

  1. oneuijs/You-Dont-Need-jQuery
  2. iOS实现自定义进度条、拖动条效果,可多个
  3. RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
  4. C#设计模式(11)——外观模式(Facade Pattern)
  5. 微信小程序实例-获取当前的地理位置、速度
  6. C Primer Plus 第3章 数据和C 编程练习
  7. iOS-图片png
  8. STL--string(转载)
  9. root用户安装的软件在普通用户不生效
  10. C++ do while 0 使用和含义
  11. Eclipse编译运行没问题,但执行mvn clean install跑单元测试失败的原因解析
  12. linux下安装FTP详细
  13. java使用指定的国际化文件
  14. PHP数组总结,,PHP面向对象思维思路。
  15. OOAD之单例模式Singleton的6种写法
  16. [SDOI2009]HH的项链(莫队)
  17. BZOJ 3731: Gty的超级妹子树
  18. 解决命令行执行shell脚本成功,但crontab执行失败
  19. JavaMath方法、服务器与Tomcat安装与配置步骤
  20. [IC]Lithograph(1)光刻技术分析与展望

热门文章

  1. Install kubernetes without yum
  2. java中BufferedImage类的用法
  3. c#md5加密的简单用法
  4. IOS 苹果手机fiddler抓包时出现了tunnel to 443 解决方案,亲测有效
  5. spring使用@Value标签读取.properties文件的中文乱码问题的解决
  6. MVC HTML页面使用
  7. git review filter的一些规则
  8. gitlab服务器迁移
  9. Codeforces 825D Suitable Replacement - 贪心 - 二分答案
  10. 2018年12月7日 字符串格式化2 format与函数1