Catch That Cow

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67   Accepted Submission(s) : 22
Problem 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
 
Source
PKU
 
 
题意:牛逃跑了,人要去抓回来这只牛,他们在一条直线上 ,现在牛的坐标是K,人的坐标是N,牛呆在原位置不动。人有两种行进方式,步行和传送,步行可以向前或向后走一步,传送为到达人当前坐标*2 的位置。每行进一次用一分钟,问人最少需要几分钟可以抓到牛。
 
思路:
 
代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn=; int vis[maxn+];
int n,k; struct node
{
int x,c;
}; int BFS()
{
queue<node> q;
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
node cur,next;
cur.x=n,cur.c=;
vis[cur.x]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=; i<; i++)
{
if(i==)
next.x=cur.x-;
else if(i==)
next.x=cur.x+;
else
next.x=cur.x*;
next.c=cur.c+;
if(next.x==k)
return next.c;
if(next.x>= && next.x<=maxn && !vis[next.x])
{
vis[next.x]=;
q.push(next);
}
}
}
return ;
} int main()
{ freopen("1.txt","r",stdin); while(~scanf("%d%d",&n,&k))
{
if(n>=k)
{
printf("%d\n",n-k);
continue;
}
printf("%d\n",BFS());
}
return ;
}

***参考Catch That Cow(BFS)的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

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

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

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

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

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

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

  6. poj 3278 catch that cow BFS(基础水)

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

  7. 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 ...

  8. POJ3278 Catch That Cow —— BFS

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

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

随机推荐

  1. mac安装lavaral

    1,安装composer # brew install josegonzalez/php/composer

  2. Redis同步操作失败的原因

    今天弄了下 Redis 的主从同步,设置方法其实很简单的,但崩溃的是遇到个莫名其妙的问题,始终同步不了.. 看了看错误日志: Unable to connect to MASTER: Invalid ...

  3. ubuntu16.04 禁用Guest用户

    .打开终端(快捷键 Ctrl+Alt+T) .编辑50-no-guest.conf文件,按照以下命令编辑, sudo gedit /usr/share/lightdm/lightdm.conf.d/- ...

  4. CoreAnimation的使用

    一.CABasicAnimation CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"posi ...

  5. nyoj 523 双向广搜

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...

  6. <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数

     蛇形填数: 在n×n方阵里填入1,2,....,n×n,要求填成蛇形.例如,n = 4 时方阵为:    10  11  12  1   9  16  13  2 8  15  14  3 7   ...

  7. 阅读国外大神对this的分析,自己的总结

    大神的分析地址:http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/#comment- ...

  8. 学习笔记(C++Primer)--易错点总结(Chapter2)

    2.1.2Type Conversions(1/10/2017) 1.If we assign an out-of-range value to an object of unsigned type, ...

  9. php浏览历史记录的方法

    本文实例讲述了php浏览历史记录的方法.分享给大家供大家参考.具体实现方法如下: /** * 商品历史浏览记录 * $data 商品记录信息 */private function _history($ ...

  10. python 在mongo 中建立索引

    import pymongo mongo = pymongo.Connection('localhost') collection = mongo['database']['user'] collec ...