Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 61826   Accepted: 19329

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
vis[]; struct node{
int x;
int dis; };
node u,v; int bfs(int start,int end){
u.x=start;
u.dis=;
queue<node>q;
vis[start]=true;
q.push(u);
while(!q.empty()){
u=q.front();
q.pop();
if(u.x==end)
return u.dis;
for(int i=;i<;i++){
if(i==)
v.x=u.x+;
else if(i==)
v.x=u.x-;
else if(i==)
v.x=u.x*;
if(!vis[v.x]&&v.x>=&&v.x<=){
vis[v.x]=true;
v.dis=u.dis+;
q.push(v);
}
} }
} int main(){
int start,end;
while(scanf("%d%d",&start,&end)!=EOF){
memset(vis,false,sizeof(vis));
int step=bfs(start,end);
printf("%d\n",step);
}
return ;
}

poj 3278 catch that cow BFS(基础水)的更多相关文章

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

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

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

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

  4. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  5. poj 3278 Catch That Cow bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  6. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  7. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. OpenLayers学习笔记2——坐标转换问题

    参照别人的添加marker的demo来改造时,发现无论怎样更改经纬度,都是停留在同一个位置.过了一两天突然想起可能是坐标参考的问题,尝试搜了一下,果然是这个问题.问题是这样子的: WMTS中地图的坐标 ...

  2. 【转】VS2010发布、打包安装程序(超全超详细)

    1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...

  3. html5shiv.js的作用是

    解析 html5shiv主要解决HTML5提出的新的元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式.让CSS 样式应用在未知元素上只需执行 document.cre ...

  4. vue列表过渡效果

    <transition-group></transition-group> ① 列表 <transition-group> </transition-grou ...

  5. windows下安装win7虚拟机并安装oracle

    一.win7虚拟机 与安装linux虚拟机没有什么不同,不同的是选择客户机操作系统.内存.磁盘容量,以及映像文件. 创建win7虚拟机步骤简化: 新建虚拟机-->>自定义-->> ...

  6. 列表与特殊字符,div(新手HTMLL基础)

    1.无序列表 -项目符号:实心圆(disc).方框(square).空心圆(circle) -列表<ul>---- 列表项<li>--- </li></ul& ...

  7. ASIHTTPRequest的使用

    本文转自csdn ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中. ...

  8. MySql学习笔记04

    复习 day01 数据库相关: create database db4 character set utf8; show databases; show create database db4; dr ...

  9. 正则表达式通用匹配ip地址及主机检测

    在使用正则表达式匹配ip地址时如果不限定ip正确格式,一些场景下可能会产生不一样的结果,比如ip数值超范围,ip段超范围等,在使用正则表达式匹配ip地址时要注意几点: 1,字符界定:使用  \< ...

  10. win10下安装mysql-5.7.23-winx64

    Step1 官方下载地址 https://dev.mysql.com/downloads/mysql/ 选择手动下载版本 解压到自己指定的路径 上图中的my.ini及data文件夹在压缩包里是没有的, ...