bfs—Catch That Cow—poj3278
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 87152 | Accepted: 27344 |
http://poj.org/problem?id=3278
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?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
int pace[MAX+];
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(pace,,sizeof(pace));
int t;
queue<int> qu;
qu.push(n);
while(!qu.empty())
{
t=qu.front();
if(t==k)
{
cout<<pace[t]<<endl;
return ;
}
qu.pop();
if(t<MAX&&!pace[t+])
{
qu.push(t+);
pace[t+]=pace[t]+;
}
if(t>&&!pace[t-])
{
qu.push(t-);
pace[t-]=pace[t]+;
}
if(t*<=MAX&&!pace[t*])
{
qu.push(t*);
pace[t*]=pace[t]+;
}
}
}
return ;
}
bfs—Catch That Cow—poj3278的更多相关文章
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- POJ 3287 (基础BFS) Catch That Cow
这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ3278 Catch That Cow —— BFS
题目链接: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: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
随机推荐
- Netty 中的 handler 和 ChannelPipeline 分析
上一节我们讲了 Netty 的启动流程,从启动流程入手分析了 Reactor 模型的第一步:channel 如何绑定 Selector.然后讲到了 EventLoop 在启动的时候发挥了什么作用.整个 ...
- ACL,NAT的使用
项目练习 练习一: 练习目的:通过配置路由器的dhcp功能使pc自动获取ip地址. Router>enable Router#configure terminal Router(config) ...
- 【Web】阿里icon图标webpack插件(webpack-qc-iconfont-plugin)详解
webpack-qc-iconfont-plugin webpack-qc-iconfont-plugin是一个webpack插件,可以轻松地帮你将阿里icon的图标项目下载至本地 开发初衷 之前已经 ...
- MTK Android MCC(移动国家码)和 MNC(移动网络码)
国际移动用户识别码(IMSI) international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码. 从技术上讲,IMSI可以彻底解决国际漫游问 ...
- (js描述的)数据结构[哈希表1.3](10)
1.哈希表的完善 1.容量质数(limit):需要恒为质数,来确保元素的均匀分布. 1)普通算法: 判断一个数是否为质数 function isPrime(num) { for (var i = 2; ...
- 计算机网络协议,UDP数据报的分析
一.UDP数据报的特点 1.基本特性 UDP是在IP数据报的基础上增加了复用和分用以及差错检测的功能 UDP的主要特点如下: UDP是无连接的:即发送数据之前不需要建立连接 UDP使用尽最大努力交付, ...
- 在IDEA中搭建Java源码学习环境并上传到GitHub上
打开IDEA新建一个项目 创建一个最简单的Java项目即可 在项目命名填写该项目的名称,我这里写的项目名为Java_Source_Study 点击Finished,然后在项目的src目录下新建源码文件 ...
- 如何改变Xcode字体大小?
运行Xcode后依次点击左上角Xcode/Preferences/Fonts & Colors里就可以调整,在右边随便点中一个字体就可以调整这个字体的大小和颜色了,按command+a可以将所 ...
- Python 注释(Python Comments)用法详解
目录 1 Python 注释概述 2 Python 注释的作用 2.1 调试代码 2.2 提高程序的可读性 3 Python 单行注释 3.1 Python 单行注释概述 3.2 单行注释注释单行代码 ...
- JMeter在Mac下的安装
其实不论操作系统是Windows.Unix(如Mac OS).Linux(如Ubuntu)等,JMeter所需要的基础环境配置都是类似的,本文介绍JMeter for MAC的安装与环境配置. JMe ...