POJ 3278 Catch That Cow(赶牛行动)
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 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?
农夫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(赶牛行动)的更多相关文章
- BFS POJ 3278 Catch That Cow
		
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
 - 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 ...
 - POJ 3278 Catch That Cow(BFS,板子题)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
 - poj 3278:Catch That Cow(简单一维广搜)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
 - POJ 3278 Catch That Cow(bfs)
		
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
 - poj 3278 Catch That Cow (bfs搜索)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
 - POJ 3278 Catch That Cow[BFS+队列+剪枝]
		
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
 - poj 3278 catch that cow  BFS(基础水)
		
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
 - 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 ...
 
随机推荐
- Tomcat JAR包冲突报错
			
查看Tomcat下有两个PDF加密的jar包如图: 删除这个bcprov-jdk14-138.jar包,然后重启Tomcat就好了. 这个jar包和Tomcat中的一个包冲突,反复调用导致的. 参考: ...
 - 国外互联网大企业(flag)的涨薪方式
			
国外互联网大企业(flag)指的是:Facebook,Google,Amazon,LinkedIn 至于 A 代表哪家公司存在争议:有人说是Amazon,也有说是Apple,现在更有人说应该是AirB ...
 - 单台主机上DB2 10.5和arcgis 10.4 空间数据库配置
			
该篇文章重点参考arcgis官网说明:http://enterprise.arcgis.com/zh-cn/server/10.4/publish-services/linux/register-db ...
 - scala 操作hdfs
			
获取hdfs文件下所有文件getAllFiles 遍历 spark读取 1 package com.spark.demo import java.io.IOException import java. ...
 - Oracle误删除数据恢复
			
select * from tablename as of timestamp to_timestamp('2018-05-04 13:30:00','yyyy-MM-dd hh24:mi:ss') ...
 - 基于ARM Cortex-M0+ 的Bootloader 参考
			
源: 基于ARM Cortex-M0+内核的bootloader程序升级原理及代码解析
 - Mysql去掉html标签函数
			
函数 SET GLOBAL log_bin_trust_function_creators=; DROP FUNCTION IF EXISTS fnStripTags; DELIMITER | CRE ...
 - centos6安装多实例mysql
			
centos 6.5,使用二进制安装多实例mysql 5.5.60 所需安装包mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz.ncurses-devel-5.7- ...
 - Mysql安装错误:Install/Remove of the Service Denied!解决办法
			
Mysql安装错误:Install/Remove of the Service Denied!解决办法 在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld -- ...
 - MD5算法详解
			
MD5是什么 message-digest algorithm 5(信息-摘要算法).经常说的“MD5加密”,就是它→信息-摘要算法.在下载一下东西时,经常在一些压缩包属性里,看到md5值.而且这个下 ...