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.

题意:a走到b,3种移动方式。
题解:搜索。
AC代码:
#include<string.h>
#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;int road[200000];int main()
{queue<int>q, step;int n,k,x0,st0;while (scanf("%d%d", &n, &k) != EOF){memset(road, -1, sizeof(road));while (q.size()) { q.pop(); step.pop(); }q.push(n);step.push(0);//road[n] = 0;while (q.size()){x0 = q.front();q.pop();st0 = step.front();step.pop();road[x0] = st0;if (x0 == k) { printf("%d\n", road[k]);break; }if (x0 - 1 >= 0 && road[x0 - 1] < 0) { q.push(x0 - 1);step.push(st0 + 1); }if (x0 +1 <= 100000 && road[x0 + 1] < 0) { q.push(x0 + 1);step.push(st0 + 1); }if (x0*2<=100000&& road[x0 *2] < 0) { q.push(x0 *2);step.push(st0 + 1); }}}
}

转载于:https://www.cnblogs.com/914295860-jry/p/5822511.html

POJ 3278 Catch That Cow相关推荐

  1. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  2. BFS POJ 3278 Catch That Cow

    题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...

  3. POJ 3278 Catch That Cow BFS

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32071   Accepted: 9866 D ...

  4. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  5. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35043   Accepted: 10800 ...

  6. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  7. bfs+dfs分析----poj 3278 Catch That Cow

    题目详情 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 115430   Accepted:  ...

  8. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  9. poj 3278 Catch That Cow(广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 ...

  10. POJ - 3278 Catch That Cow 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

最新文章

  1. [原创]SQL 表值函数:获取从今天计算起往前自定义天数
  2. skb_shinfo(skb)-dataref 含义
  3. Java 8大原子操作
  4. action 带参数跳转
  5. SpringBoot学习之路:06.Spring Boot替换默认的Jackson
  6. [Codeforces Round #165 (Div. 2)]D. Greenhouse Effect
  7. 防止在多模块Maven中找到“未找到插件”
  8. 字符串中全角半角之间的转换
  9. 个人房贷为啥又贵又难贷 一个房贷银行有3套逻辑
  10. SVN遇到Can't convert string from 'UTF-8' to native encoding(转)
  11. python找不到文件中文文件名_找不到的方法虽然存在于同一个py文件中 - python
  12. vs资源视图加载失败
  13. OpenCV 访问Mat 像素
  14. 学python买什么书-Python爬虫入门看什么书好 小编教你学Python
  15. Ubuntu1404+Django1.9+Apache2.4部署配置1安装
  16. ignite mysql 持久化_2 ignite关键特性
  17. python实现isprime函数_Python实现isPrime函数
  18. 【JZOJ5336】【NOIP2017提高A组模拟8.24】提米树
  19. 声谱图(spectrogram)、FBank(Mel_spectrogram)和 MFCC(Mel倒谱)到底用哪个作为NN输入?
  20. diffuse、specular贴图的光照

热门文章

  1. 国内现在web前端高手薪资都拿多少?
  2. vue.js是什么框架?有什么优势?
  3. android在xml中加载大图,android – 如何在布局xml中添加循环视图
  4. props传递对象_vue-父组件传值props(对象)给子组件
  5. python实时连接oracle_python连接oracle数据库
  6. mysql跳板机_python实现ssh通过跳板机连接mysql
  7. jpa onetoone_java – JPA,Hibernate:仅使用外键的OneToOne映...
  8. PyTorch: torch.optim 的6种优化器及优化算法介绍
  9. TensorFlow神经网络(七)卷积神经网络基础
  10. scrapy 去重策略修改