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. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  2. netty初探(2)

    上一篇 netty(1) 一.TCP/IP 流式传输 在上文演示了2进制流式传输引起的TCP拆包问题,这里继续演示文本型的传输问题,文本型的可以有以下几种策略 1.1 以特殊字符表示结尾 HTTP协议 ...

  3. Dynamics CRM 相关资料

    links: 1.The Microsoft Dynamics CRM Team Blog 2.申请试用Dynamics CRM 2013 http://www.microsoft.com/zh-cn ...

  4. Flexslider图片轮播、文字图片相结合滑动切换效果

    Flexslider是一款基于的jQuery内容滚动插件.它能让你轻松的创建内容滚动的效果,具有非常高的可定制性.开发者可以使用Flexslider轻松创建各种图片轮播效果.焦点图效果.图文混排滚动效 ...

  5. LeetCode 328. Odd Even Linked List C#

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  6. 用Karma和Jasmine测试Angular应用

    TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...

  7. sqlserver负载均衡

    http://www.cnblogs.com/gaizai/p/3644510.html

  8. 解决maven web项目Cannot detect Web Project version. Please specify version of Web Project through...的错误

    前面已经创建maven web工程,但是问题来了,创建maven web工程之后会出现如下的错误,在pom.xml文件头部 有以下的错误 Description Resource Path Locat ...

  9. adb shell dumpsys

    adb shell dumpsys activity activities -- class/packagename adb shell dumpsys batterystate --reset   ...

  10. linux下卸载和安装mysql数据库的方法

    1.1  MySQL下载 下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads 版本:5.1.68 平台:linux general ...