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 ...
随机推荐
- linux 远程执行命令
命令: ssh 命令参数: -l 指定登入用户 -p 设置端口号 -f 后台运行,并推荐加上 -n 参数 -n 将标准输入重定向到 /dev/null,防止读取标准输入 -N 不执行远程命令,只做端口 ...
- sql server 中后缀为.mdf的文件是干什么用的??
在微软的SQL Server 2000 数据库有三种类型的文件: 1)主要数据文件(扩展名.mdf是 primary data file 的缩写) 主要数据文件包含数据库的启动信息,并指向数据库中的其 ...
- VPS采用的几种常见技术(OVZ、Xen、KVM)介绍与对比
很多人看到同样配置的VPS价格相差很大,甚是不理解,其实VPS使用的虚拟技术种类有很多,如OpenVZ.Xen.KVM.Xen和HVM与PV. 在+XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用 ...
- eclipse中的tomca的编辑页面server location灰色
clipse中tomcat service设置 选择window ----show view---services可以看到服务的面板 双击tomcat进入配置界面Service Locations(S ...
- 专题8:javascript函数详解
函数是一段可以反复调用的代码块.函数还能接受输入的参数,不同的参数会返回不同的值. 函数的声明 JavaScript 有三种声明函数的方法. (1)function 命令 function命令声明的代 ...
- 前端框架VUE----node.js的简单介绍
一.什么是node.js? 它是可以运行JavaScript的服务平台,可以吧它当做一门后端程序,只是它的开发语言是JavaScript 二.安装 1.node.js的特性: - 非阻塞IO模型 - ...
- Java连接数据库 #02# JDBC经典套路
内容索引 LocalConnectionFactory.java LocalConnectionProxy.java ProfileDAO.java-2.0 ProfileDAOImpl.java-2 ...
- python2.7安装mysql-python
环境:python2.7.14+mysql-python-1.2.3.win-amd64-py2.7.exe 安装版本许参照解释器版本,有64和32位之分,如果安装错误版本,则运行时会报错:Impor ...
- nagios监控oracle 表空间
oracle表空间满的危害以及处理方式见我的博客链接https://www.cnblogs.com/-abm/p/9764803.html 除此之外我们还需要对表空间实时监控,这样就可以及时了解表空间 ...
- 安装kali linux 2017.1 【一、更新源 升级软件和内核等】
呃,我这次安装是在VMware WorkStation里进行的,具体操作系统安装步骤不谈. ISO文件下载地址:https://www.kali.org/downloads/ 说下安装后的升级操作.下 ...