Catch That Cow
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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
 
简单的bfs,题目是单测试例,写的是多测试例。
 #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的更多相关文章

  1. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  2. POJ 3287 (基础BFS) Catch That Cow

    这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节 ...

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

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

  4. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  5. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  6. POJ3278 Catch That Cow —— BFS

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

  7. POJ 3278 Catch That Cow(bfs)

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

  8. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  9. 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 ...

随机推荐

  1. 【Linux】网络应用

    一. 网络基本配置 setup 永久网络设置 (IP 网关 DNS) vim /etc/sysconfig/network-scripts/ifcfg-eth0 (网卡信息文件) route -n 查 ...

  2. uni-app在线引入阿里字体图标库

    第一步 在app.vue中引入阿里字体图标库 第二步 在任意页面使用就可以了 <view class="item" v-for="(value,index) in ...

  3. EntityFramework Core 3.x上下文构造函数可以注入实例呢?

    前言 今天讨论的话题来自一位微信好友遇到问题后请求我的帮助,当然他的意图并不是本文标题,只是我将其根本原因进行了一个概括,接下来我们一起来探索标题的问号最终的答案是怎样的呢? 上下文构造函数是否可以注 ...

  4. 搭建DVWA Web渗透测试靶场

    文章更新于:2020-04-13 按照惯例,需要的文件附上链接放在文首. 文件名:DVWA-1.9-2020.zip 文件大小:1.3 M 文件说明:这个是新版 v1.9 (其实是 v1.10开发版) ...

  5. Linux网络架设篇,虚拟机l系统中网卡设备名与配置文件不符如何处理?

    很多情况下,当我们在虚拟机中安装好linux系统后,并不能成功连上网.当我们配置好相关IP地址后同样不能成功连接网络.并且会体会网卡名与配置名不符,这时候应该怎么办呢? 1.清空下面文件 /etc/u ...

  6. 基于 Jepsen 来发现几个 Raft 实现中的一致性问题(2)

    Nebula Graph 是一个高性能.高可用.强一致的分布式图数据库.由于 Nebula Graph 采用的是存储计算分离架构,在存储层实际只是暴露了简单的 kv 接口,采用 RocksDB 作为状 ...

  7. 通过Java HTTP连接将网络图片下载到本地

    通过Java HTTP连接将网络图片下载到本地   只知道浏览器使用的是HTTP协议,那么如何将网络资源使用JavaHTTP下载下来呢! 这只是一个非常简单的小示例,只是不想每次碰到关于此方面的内容忘 ...

  8. java网络编程socket\server\TCP笔记(转)

    java网络编程socket\server\TCP笔记(转) 2012-12-14 08:30:04|  分类: Socket |  标签:java  |举报|字号 订阅     1 TCP的开销 a ...

  9. python调用小豆机器人实现自己的机器人!

    大家好,人工智能是不是很酷呢? 今天我们用python调用小豆机器人实现自己的机器人(可以结合往期的语音识别更酷哦) 好,废话不多说直接上代码 import requests i=input(&quo ...

  10. 移动端Vue组件库-Vant学习

    全局引入 import Vant from 'vant'; //嫌麻烦就全部一次导出,虽然包会稍微有点大 import 'vant/lib/index.css'; //注意导入全局的这个css,否则布 ...