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,开始考虑模拟,后来发现条件很多要考虑的东西太多了,就用了bfs
然后就是想界限问题,开始觉得要尽量小,后来看了别人代码再看数据觉得直接用题目中的数据做界限可以出结果
接着是各种调bug,其实方法一开始就写对了,但因为中间一两行代码的错误和没有发现导致不停的错
唉~今天的第二次了,第一次调了半个多小时,第二次直接调了一个半小时
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 100010
int n,k,vis[maxn];
struct node{
int x,step;
};
void bfs(){
node p;
p.x = n,p.step = ;
vis[n] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == k){
cout << tmp.step << endl;
return;
}
for(int i=;i<;i++){
int xx;
if(i == ){
xx = tmp.x + ;
}
else if(i == ){
xx = tmp.x - ;
}
else xx = tmp.x * ;
if(xx < || xx > maxn){
continue;
}
if(!vis[xx]){
vis[xx] = ;//注意vis数组
node tp;//这里新建一个结构体对象,不要在原来的tmp上加减!!! 在这里调了一个半小时!!!
tp.x = xx;
tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main(){
while(cin >> n >> k){
memset(vis,,sizeof(vis));
if(n<k){
bfs();
}
else{
cout << n - k << endl;
}
}
return ;
}

Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索的更多相关文章

  1. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  2. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. 棋盘问题 POJ - 1321 [kuangbin带你飞]专题一 简单搜索

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  4. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  7. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  8. [kuangbin带你飞]专题一 简单搜索 x

    A - 棋盘问题 POJ - 1321 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋 ...

  9. [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

    //Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...

随机推荐

  1. javascript基础案例解析

    学完了JavaScript基础部分,总结出一些基本案例,以备日后查看! 1.九九乘法口诀表:在控制台中输出九九乘法口诀表!代码如下: <!DOCTYPE html> <html> ...

  2. Java虚拟机学习笔记(二)--- 判断对象是否存活

    Java堆中存放着所有的对象实例,垃圾收集器在堆进行回收之前,需要判断对象是“存活”还是“死亡”(即不可能再被任何途径引用的对象). 最常见的一种判断对象是否存活算法是引用计数算法, 给对象加一个引用 ...

  3. curl工具使用实例

    curl是一个命令行工具,其基于libcurl库,用于发送网络请求,获取并展示响应数据,下面来看curl的具体用法. 1.下载网页源码 curl命令直接接URL,用于下载指定URL的网页源码,并将其显 ...

  4. python之闭包+装饰器

    闭包 内部函数对外部函数作用域变量的引用. 函数内的属性都是有生命周期的,都是在函数执行期间 闭包内的闭包函数私有化了变量,类似于面向对象 图片解析 示例一 https://www.bilibili. ...

  5. Tomcat中文乱码问题

    新从官网下载的Tomcat7和Tomcat8,在运行的时候都会有乱码的问题,就此发现问题,我们就给它就地正法! 经过初步的分析,问题产生的大概原因是由于Tomcat的log日志模块不识别中文的问题, ...

  6. Unity进阶之ET网络游戏开发框架 03-Hotfix层启动

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  7. 云片RocketMQ实战:Stargate的前世今生

    RocketMQ消息队列,专业消息中间件,既可为分布式应用系统提供异步解耦和削峰填谷的能力,同时也具备互联网应用所需的海量消息堆积.高吞吐.可靠重试等特性,是应对企业业务峰值时刻必备的技术. 云片由于 ...

  8. Go-json解码到结构体-踩坑

    package main import ( "encoding/json" "fmt" ) type User struct { Name string `js ...

  9. spring-boot-plus项目打包(七)

    spring-boot-plus项目打包 项目打包 spring-boot-plus项目使用maven assembly插件进行打包 根据不同环境进行打包部署 包含启动.重启命令,配置文件提取到外部c ...

  10. v语言怎么玩

    直接上github: https://github.com/vlang/v 前戏 大概是在6月份的时候,在github上看到了这个玩意,我以为是??? 我下意识的去查了一下有没有人在讨论这个语言,但是 ...