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. centos下的安装mysql,jdk

    mysql: 如果你是用rpm安装, 检查一下RPM PACKAGE:rpm -qa | grep -i mysql如果mysql已经安装在本机,则会列出mysql安装过的文件 ,像mysql-ser ...

  2. window下安装ubuntu(ubuntu可删除)

    进入ububtu13.04的安装界面,这里我们选择了“中文(简体)”,然后单击安装: 下图是现场拍的:   出现如下图时,请根据需要选择,然后单击“继续” , 接下来会出现问你是否要连接网络,我们选择 ...

  3. 启动Jmeter时遇到的几种错误

    1.权限不够 解决办法:用管理员权限运行 2.sdk版本太低 解决办法:1)查看当前sdk版本:java -version 2)安装sdk1.7或以上版本(jmeter3.0版本要用sdk1.7及以上 ...

  4. java基础编程——获取栈中的最小元素

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)).   题目代码 /** * Created by YuKai Fan on 2018/9 ...

  5. css布局:块级元素的居中

    一.定宽: 1.定位居中(absolute) 方法一: html: <div class="main"></main> css: .main{ width: ...

  6. 升级win10后电脑经常自动重启的问题

    升级win10后用户体验度确实比win7强了很多,但是电脑无故的重启,让人无法接受,下面就介绍win10电脑自动重启问题的解决方案 问题分析: 遇到这种情况主要是硬件与系统不兼容所致 解决方案: 1, ...

  7. JAVA JDBC 连接 Oracle

    使用 Junit 测试类编写 public class JdbcTest { private Connection con = null;// 创建一个数据库连接 private PreparedSt ...

  8. iView - Form中想要重置DatePicker生效,必须给DatePicker绑定value属性

    Form中想要重置DatePicker生效,必须给DatePicker绑定value属性

  9. Problem I. Count - HDU - 6434(欧拉函数)

    题意 给一个\(n\),计算 \[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]\] 题解 令\(a = i - j\) 要求 \[\sum ...

  10. Codeforces Round #456 (Div. 2) B. New Year's Eve

    传送门:http://codeforces.com/contest/912/problem/B B. New Year's Eve time limit per test1 second memory ...