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.
 
求n到k需要多少步变化有(n+1,n-1,n*2)三种选择;
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
#define N 100010 int m,n;
int vis[N];
struct node
{
int x,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
}; int dfs()
{
priority_queue<node>Q;
memset(vis,,sizeof(vis));
node q,s;
s.x=n;
vis[s.x]=;
s.step=;
Q.push(s);
int i;
while(!Q.empty())
{
q=Q.top();
Q.pop();
if(q.x==m)
return q.step;
for(i=;i<;i++)
{
if(i==)
s.x=q.x+;
else if(i==)
s.x=q.x-;
else if(i==)
s.x=q.x*;
if(s.x<&&s.x>=&&vis[s.x]==)//vis[s.x]==0必须放到后面,-_-被运行错误错了好多次;
{
vis[s.x]=;
s.step=q.step+;
Q.push(s);
} }
}
return -;//要有返回值,我也不知道为什么;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==n)
{
printf("0\n");
continue;
}
int ans;
ans=dfs();
printf("%d\n",ans);
}
return ;
}

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. flask获取传入参数的两种方式

    #coding=utf-8 from flask import Flask from flask import request app = Flask(__name__) @app.route(&qu ...

  2. inux跟踪线程的方法:LWP和strace命令

    摘要:在使用多线程程序时,有时会遇到程序功能异常的情况,而这种异常情况并不是每次都发生,很难模拟出来.这时就需要运用在程序运行时跟踪线程的手段,而linux系统的LWP和strace命令正是这种技术手 ...

  3. MVC的路由设置【转】

    转,MVC的路由设置. 后端获取路由里面action的参数,函数需要设置相同的参数名称才行. routes.MapRoute( "Default", "{controll ...

  4. JAVA对URL的解码【转】

    前段时间做URL的中文转换,有些url是utf8的格式,有的是gb2312的格式,很难区分到底是utf8还是gb2312,找了好久,发现网上的一个牛人写的转换代码: package org.apach ...

  5. 项目适配iOS9遇到的一些问题及解决办法(更新两个小问题)

    本文转载至 http://www.bubuko.com/infodetail-1110714.html http://www.jianshu.com/p/631bd7f12a38 1.网络请求报错.升 ...

  6. Sqlserver 数据库、表常用查询操作

    查询所有表以及记录数: select a.name as 表名,max(b.rows) as 记录条数 from sysobjects a ,sysindexes b where a.id=b.id ...

  7. android 线程间的通信

    (转自:http://www.cnblogs.com/allin/archive/2010/05/19/1738800.html) andriod提供了 Handler 和 Looper 来满足线程间 ...

  8. 题目1040:Prime Number(第k个素数)

    题目链接:http://ac.jobdu.com/problem.php?pid=1040 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. sencha touch 在线实战培训 第一期 第五节

    2014.1.6晚上8点准时开的课 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容:             实现皮肤自定义样式的修改 css相关理论: 重写官方样式,美化ap ...

  10. jpa中时间戳格式应该用哪种类型

    遇到个bug,数据库时间存储用了datetime,但是下面的java jpa代码,查询回来,却只有日期. String innerSql = getInnerQuery(departmentId, k ...