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 ;
}

POJ 3278 Catch That Cow(赶牛行动)的更多相关文章

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  3. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  4. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  7. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  8. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  9. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

随机推荐

  1. JsonPath:从多层嵌套Json中解析所需要的值

    问题 应用中,常常要从嵌套的JSON串中解析出所需要的数据.通常的做法是,先将JSON转换成Map, 然后一层层地判空和解析.可使用 JsonPath 来解决这个问题. 给定一个 JSON 串如下所示 ...

  2. USMART 组件移植到STM32

    USMART是由ALIENTEK开发的一个串口调试助手组件,通过它可以通过串口调试助手,调用程序里面的任何函数并执行,单个函数最多支持10个输入参数,并支持函数返回值显示. USMART支持的参数类型 ...

  3. 20165215 2017-2018-2 《Java程序设计》第4周学习总结

    20165215 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 chapter5 子类与父类 子类的定义使用关键字extends 任何类都是Object类的 ...

  4. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  5. RBAC

    什么是rbac? -- 基于角色的权限控制  Role-Based Access Control 一个url就代表一个权限 // url分配给角色,角色分配给用户 -- 6个model,4张表 菜单表 ...

  6. [转载]dbms_lob用法小结

    http://blog.sina.com.cn/s/blog_713978a50100prkt.html CLOB里存的是2进制 判定长度   DBMS_LOB.GETLENGTH(col1)获取文本 ...

  7. 大数据自学2-Hue集成环境中使用Sqoop组件从Sql Server导数据到Hive/HDFS

    安装完CDH后,发现里面的东东实在是太多了,对于一个初学大数据的来说就犹如刘姥姥进了大观园,很新奇,这些东东每个单拿出来都够喝一壶的. 接来来就是一步一步地学习了,先大致学习了每个模组大致做什么用的, ...

  8. Java 代码性能调优“三十六”策

    代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...

  9. H5+JS生成验证码

    效果图如下: <canvas id="canvas1" style="margin-left: 200px;"></canvas>< ...

  10. vs2015 dx15开发教程一