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 - 1 or + 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,三个状态分别为x-1,x+1,2*x,注意剪枝一些不太可能的状态,代码如下:
const int maxm = ;

struct Node {
int times, x;
Node(int _times, int _x):times(_times),x(_x) {}
}; int vis[maxm], n, k; int main() {
scanf("%d%d", &n, &k);
queue<Node> q;
q.push(Node(, n));
while(!q.empty()) {
Node tmp = q.front();
q.pop();
if(vis[tmp.x])
continue;
vis[tmp.x] = ;
if(tmp.x == k) {
printf("%d\n", tmp.times);
break;
}
tmp.times++;
if(tmp.x && !vis[tmp.x-])
q.push(Node(tmp.times, tmp.x - ));
if(!vis[tmp.x+])
q.push(Node(tmp.times, tmp.x + ));
if(tmp.x < k * && !vis[tmp.x*])
q.push(Node(tmp.times, tmp.x * ));
}
return ;
}

Day2-E-Catch That Cow-POJ3278的更多相关文章

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

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

  2. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

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

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

  4. poj3278 Catch That Cow

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

  5. POJ3278——Catch That Cow(BFS)

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

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

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

  7. POJ3278 Catch That Cow —— BFS

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

  8. POJ3278——Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 114140   Accepted: 35715 ...

  9. POJ 3278 Catch That Cow(bfs)

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

  10. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

随机推荐

  1. AgileReview 代码检视工具使用

    AgileReview 它是一个eclipse插件. http://www.agilereview.org/ 官网地址. 四种下载插件方式. 1.Marketplace QuickInstall    ...

  2. Linux 笔记:虚拟控制台

    登录后按Alt+F2键这时又可以看到"login:"提示符,这个就是第二个虚拟控制台. 一般新安装的Linux有四个虚拟控制台,可以用Alt+F1~Alt+F4来访问. 虚拟控制台 ...

  3. pycharm/IDEA等 windows 版 常用快捷键

  4. 《创业者技能树》--创业课--14天复盘第一课candy2--HHR计划

    1,厉害的人是如何分析问题的?

  5. Kubernetes的控制器之Deployment的定义

    Deploy 的控制器定义参数介绍 [root@master manifests]# kubectl explain deploy KIND: Deployment VERSION: extensio ...

  6. jQuery学习(三)

    jQuery文档操作方法 1.内部追加内容 选择器追加到内容 append(content)在当前jQuery对象内部所包含的DOM对象的内部的最后追加content对应的内容,其中content可以 ...

  7. 思科室外AP无法注册到WLC

    思科的一些新的室外AP,在购买回来时,有时候会出现无法加入WLC的情况,现象基本是无法加入,或感觉加入了,立马又掉了. 例如: AIR-AP1562E-H-K9 AIR-AP1572EAC-H-K9 ...

  8. JAVA 通过POI 模版,导出excel

    如有不足,欢迎指正,谢谢 ! 1.Maven引入  POI jar包.模版和结果文件.rar下载 <dependency> <groupId>org.apache.poi< ...

  9. centos7.4安装gitlab

    1. 安装依赖软件 yum -y install policycoreutils openssh-server openssh-clients postfix 2.下载gitlab安装包,然后安装 c ...

  10. ios中时间倒计时

    博客地址 https://github.com/sundayios/SQCountTimeDown.git