Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 78114   Accepted: 24667

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

Source

 
 #include "iostream"
#include "fstream"
#include "sstream"
#include "cstdio"
#include "queue" using namespace std ;
const int maxN = 1e5 + 1e3 ;
const int Max_Limit = 1e5 ;
const int INF = ;
typedef long long QAQ ; queue < int > Q ;
bool vis[ maxN ] ;
int step[ maxN ] ;
int K , N ; bool Check ( int x_x ) { return x_x == K ? true : false ; } int BFS ( ) {
int temp ;
bool Get_Target = false ;
Q.push ( N ) ;
vis[ N ] = true ;
while ( !Q.empty ( ) ) {
int tmp = Q.front ( ) ;
Q.pop ( ) ;
for ( int i= ; i< ; ++i ) {
switch ( i ) {
case :{
temp = tmp - ;
break;
}
case :{
temp = tmp + ;
break;
}
case :{
temp = tmp * ;
break;
}
} if ( temp > Max_Limit || temp < ) continue ;
if ( !vis[ temp ] ) {
Q.push( temp ) ;
step[ temp ] = step[ tmp ] + ;
vis[ temp ] = true ;
if ( Check ( temp ) ) {
Get_Target = true ;
return step[ temp ] ;
}
}
}
}
if ( !Get_Target ) return - ;
} int main ( ) {
scanf ( "%d %d" , &N , &K ) ;
if ( N >= K ) {
printf ( "%d\n" , N - K ) ;
goto End ;
}
printf ( "%d\n" , BFS ( ) ) ;
End :
return ;
}

2016-10-19 21:59:12

POJ 3278 题解的更多相关文章

  1. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. BFS POJ 3278 Catch That Cow

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

  4. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  5. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  6. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  7. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  8. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

  9. POJ - 3278

    题目链接:http://poj.org/problem?id=3278 ac代码: #include <iostream>#include <stdio.h>#include ...

随机推荐

  1. 学习笔记——k近邻法

    对新的输入实例,在训练数据集中找到与该实例最邻近的\(k\)个实例,这\(k\)个实例的多数属于某个类,就把该输入实例分给这个类. \(k\) 近邻法(\(k\)-nearest neighbor, ...

  2. [NHibernate]Nhibernate如何映射sqlserver中image字段

    概述 有这样一个需求需要管理企业内网的信息,包括图标和链接.考虑到图标也不是很大所以就将图片直接保存在数据库中了. 但是用到Nhibernate,如何映射呢? Table 5.5. Large Obj ...

  3. spring mvc+mybatis+sql server简单配置

    context.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  4. PHP数组函数: array_map()

    定义和用法 array_map() 函数返回用户自定义函数作用后的数组.回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致. 语法 array_map(function,a ...

  5. 如何挂载阿里云Linux服务器的“数据盘”(新购买)

    详细操作参考: http://jingyan.baidu.com/article/90808022d2e9a3fd91c80fe9.html 用到的命令行汇总: 1.查看磁盘: fdisk -l 2. ...

  6. ASP.NET Core--授权过滤器

    翻译如下: 目前,我们正在从事这方面工作. 我们欢迎您的加入,以帮助塑造范围和方法.您可以跟踪状态并在此提供的输入问题在GitHub上. 如果你想查看初稿并在此主题的大纲,请留下注意到在您的联系信息的 ...

  7. jdbctemplate中的批量更新使用,BigDecimal与造型的联系和区别

    //jdbctemplate批量新增的使用MENU_ID_LIST是前端页面传递到后端控制层,再由控制层传到实现层的List //JdbcTemplate是spring jdbctemplate通过注 ...

  8. Android四大组件知识整理

    1. Activity 1.1 什么是Activity? Activity是Context的子类,并可以处理与窗体用户的事件: 1.2 Activity的生命周期 不存在->`onCreate( ...

  9. Unity3D NGUI刮刮卡效果

    线上效果 确保你的纹理的read/write 是勾选的,纹理格式是 RGBA32的 //代码 using UnityEngine; [RequireComponent(typeof(UITexture ...

  10. C++ 判断字符串是否全是数字

    在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取.下面介绍了判断字符串是否 ...