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. 【kafka】一、消息队列

    在高并发的应用场景中,由于来不及同步处理请求,接收到的请求往往会发生阻塞.例如,大量的插入.更新请求同时到达数据库,这会导致行或表被锁住,最后会因为请求堆积过多而触发“连接数过多的异常” 的错误.因此 ...

  2. LR有的JMeter也有之二“检查点”

    好吧!接着上一篇文章的内容和思路,继续前进. 检查点:简单的来理解一下,上一章讲到,我们对用户名和密码进行了参数化,那么怎样来判断jmeter有没有正确调用test.dat里面的文件呢.当然,我们可以 ...

  3. 佳木斯集训Day6

    T1还是个找规律啊,记下b的个数,然后直接*2%10000000009就好了 #include <bits/stdc++.h> #define mo 1000000007 using na ...

  4. 【Java例题】2.8 解一元二次方程

    8.解一元二次方程. 输入一元二次方程的a,b,c三个系数,解一元二次方程 ax^2+bx+c=0,输出两个根 package study; import java.util.Scanner; pub ...

  5. java并发编程(二十六)----ThreadLocal的使用

    其实ThreadLocal很多接触过多线程的同学都可能会很陌生,他不像current包里面那些耳熟能详的api一样在我们面前经常出现,更多的他作为一个本地类出现在系统设计里面.我们可以说一下Sprin ...

  6. abc -- 牛客

    题目描述 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc=532.求满足条件的所有a.b.c的值. 输入描述: 题目没有任何输入. 输出描述: 请输出所有满足题目条 ...

  7. python中下标和切片的使用

    下标 所谓下标就是编号,就好比超市中存储柜的编号,通过这个编号就能找到相应的存储空间. Python中字符串,列表,元祖均支持下标索引. 例如: # 如果想取出部分字符,可使用下标 name=&quo ...

  8. cs231n---语义分割 物体定位 物体检测 物体分割

    1 语义分割 语义分割是对图像中每个像素作分类,不区分物体,只关心像素.如下: (1)完全的卷积网络架构 处理语义分割问题可以使用下面的模型: 其中我们经过多个卷积层处理,最终输出体的维度是C*H*W ...

  9. .Net MVC 框架基础知识

    一.什么是MVC? MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式. MVC全名是Model View Controlle ...

  10. 基于Springboot的BaseService和BaseController

    基于Springboot的BaseService,BaseController 前言: 在做项目时需要对大量的表做增删查改,而其中的逻辑大同小异,所以抽象了一个 BaseService,BaseCon ...