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. django之视图view小知识

    CBV简版流程 AddPublisher.as_view() ——> view 函数 当请求来的时候才执行view view中执行: 1. 先实例化AddPublisher,给self def ...

  2. 利用可排序Key-Value DB构建时间序列数据库(简论)

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5855064. ...

  3. js 简易年历

    html部分 <div class='calendar'> <div class="tabBox" id='nav' > <ul> <li ...

  4. Server Library [Apache Tomcat 7.0] unbound解决方案

    问题描述: 当在MyEclipse中导入高版本Eclipse的[Eclipse Dynamic Web]项目后,会发现其Java Build Path(选定项目->Alt+Enter即可打开Pr ...

  5. js动态添加和删除table的行例子

    <table id="table_report" class="table table-striped table-bordered table-hover&quo ...

  6. ==和equasl、hashmap原理(***)

    public class String01 { public static void main(String[] args) { String a="test"; String b ...

  7. MSVCR120.dll 解决

    https://www.microsoft.com/en-us/download/details.aspx?id=40784 装之 from : https://answers.microsoft.c ...

  8. Redis可以用来做什么?(摘自http://www.lianpenglin.cc廉鹏林博客)

    Redis 是互联网技术领域使用最为广泛的存储中间件,它是「Remote Dictionary Service」的首字母缩写,也就是「远程字典服务」.Redis 以其超高的性能.完美的文档.简洁易懂的 ...

  9. Spring Boot 2 (四):使用 Docker 部署 Spring Boot

    Spring Boot 2 (四):使用 Docker 部署 Spring Boot Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常 ...

  10. mycat下mysql jdbc connector使用高版本报PacketTooBigException异常

    如下所示: 5.1.30切换为mysql-connector 5.1.46/40,报错,可参考https://blog.csdn.net/n447194252/article/details/7530 ...