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. uva658(最短路径+隐式图+状态压缩)

    题目连接(vj):https://vjudge.net/problem/UVA-658 题意:补丁在修正 bug 时,有时也会引入新的 bug.假定有 n(n≤20)个潜在 bug 和 m(m≤100 ...

  2. ZCMU Problem E: Subarray GCD(n个数的最大公约数)

    Problem E: Subarray GCD Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 44  Solved: 27[Submit][Status ...

  3. 集训考试题(CF510C Fox And Names的简化版)

    题目描述给定n个由小写字母组成的字符串,请你求出一个字母表顺序,使得这n个字符串是按照字典序升序排列的,数据保证存在合法的字母表顺序.如果存在多个解,输出字典序最小的那个. 输入格式第一行一个整数n. ...

  4. SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)

    BGSHOOT - Shoot and kill no tags  The problem is about Mr.BG who is a great hunter. Today he has gon ...

  5. UVA 839 Not so Mobile (递归建立二叉树)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归 ...

  6. 转 select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...

  7. Android技巧:查看当前界面对应的活动

    常常接手别人的android代码,非常头疼不知道界面所对应的活动是哪一个.下面所说的方法可以完美解决. 新建一个BaseActivity类,继承自AppCompatActivity,并重写onCrea ...

  8. [Hackerrank]时间转换Time Conversion

    题目链接 大致要求是说给定一个十二小时制的时间,给出它的二十四小时制的形式. 输入格式:hh:mm:ssAM 或者 hh:mm:ssPM,其中01≤hh≤12,00≤mm,ss≤59 思路 判断字符串 ...

  9. 某道我xjb想的题

    Function 时限:5s 空限:256M (都是单点) Discription 现在你有一个函数: inline int f(int x){ int tot=0,alr=0,now; while( ...

  10. Linux中安装MySql 5.7.21的详细操作步骤

    一:到mysql官网下载最新的mysql包 mysql-5.7.21-linux-glibc2.12-x86_64 官方下载地址:https://dev.mysql.com/downloads/mys ...