Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 44613   Accepted: 13946

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

题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户開始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1。2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次能够移动到牛所在的位置。所以能够用BFS来搜索这三个状态,直到搜索到牛所在的位置。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int N = 200100;
int n, k;
struct node
{
int x, step;
};
queue<node> q;
int vist[N]; void bfs()
{
int cow, ans;
while(!q.empty())
{
node tmp = q.front();
q.pop();
cow = tmp.x;
ans = tmp.step;
if(cow == k)
{
printf("%d\n",ans);
return ;
}
if(cow >= 1 && !vist[cow - 1]) //要保证减1后有意义,所以要cow >= 1 减一的情况
{
node temp;
vist[cow - 1] = 1;
temp.x = cow - 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow + 1]) //加1的情况
{
node temp;
vist[cow + 1] = 1;
temp.x = cow + 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow * 2]) //乘二的情况
{
node temp;
vist[cow * 2] = 1;
temp.x = 2 * cow;
temp.step = ans + 1;
q.push(temp);
}
}
} int main()
{
while(~scanf("%d%d",&n,&k))
{
while(!q.empty()) q.pop();
memset(vist,0,sizeof(vist));
vist[n] = 1;
node t;
t.x = n, t.step = 0;
q.push(t);
bfs();
}
return 0;
}

POJ 3278: Catch That Cow的更多相关文章

  1. POJ 3278:Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submi ...

  2. POJ 3278:The merchant(LCA&DP)

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Desc ...

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

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

  4. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  5. BFS POJ 3278 Catch That Cow

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

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

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

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

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

  8. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  9. catch that cow POJ 3278 搜索

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

随机推荐

  1. Codeforces 731 C.Socks-并查集+STL(vector+map)

      C. Socks   time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. 洛谷P1095 绝地武士的逃离

    好吧原题是守望者的逃离,我强行改了一波题面,因为信仰=-=(? May the force be with us. 绝地跑步速度为17m/s,但无法逃离荒岛.绝地的原力恢复速度为4点/s,只有处在原地 ...

  3. floyed算法的一些感想

    for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) if(f[i][k]+f[k][j]<f[i ...

  4. 学习LSM(Linux security module)之三:Apparmor的前世今生和基本使用

    感冒了,感觉一脑子浆糊,真是蛋疼. 先粗略讲一些前置知识. 一:MAC和DAC DAC(Discretionary Access Control),自主访问控制,是最常用的一类访问控制机制,意思为主体 ...

  5. ubuntu 16.04.1 LTS python 3.5.2安装

    python 3.5.2安装-----------------------apt-get -y install build-essential checkinstallapt-get install ...

  6. 【数论】【暴力】bzoj4052 [Cerc2013]Magical GCD

    考虑向一个集合里添加一个数,它们的gcd要么不变,要么变成原gcd的一个约数.因此不同的gcd只有log个. 所以对于每个位置,维护一个表,存储从这个位置向前所有的不同的gcd及其初始位置,然后暴力更 ...

  7. 【块状链表】AutSky_JadeK的块状链表模板+总结(STL版)

    Part 1.块状链表.   定位 插入 删除 数组 O(1) O(n) O(n) 链表 O(n) O(1) O(1) 对于线性表的以上常见操作来说,数组和链表都无法有效地解决.但是,若我们将链表的每 ...

  8. Bootstrap-table自定义可编辑每页显示记录数

    写在前面: 最近在做的person功能,由于后期系统中person人数较多,不利用查找person,故需求方将要求可以自己编辑每页显示的数目,而不是固定的写死每页显示的数目. 下面先来看下bootsr ...

  9. Problem Q: 零起点学算法12——求2个日期之间的天数

    #include<stdio.h> int main() { int a1,b1,c1,a2,b2,c2,s; scanf("%d-%d-%d",&a1,&am ...

  10. Android中将Bitmap转换成单色的Bmp图片

    添加权限 :