Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 73973   Accepted: 23308

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

QAQ,写循环队列wa了好久。。。默默改大队列范围。

可行性剪枝:第一,当前点在牛的左边才进行右移;第二,当前点不能为负数。

15793310

  ksq2013 3278 Accepted 1816K 32MS G++ 1159B 2016-07-23 19:37:16
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,cow;
bool vis[200100];
struct que{
int fmr,stp;
}q[201000];
int bfs()
{
int head=0,tail=1;
q[0].fmr=n;
q[0].stp=0;
vis[n]=1;
while(head^tail){
que now=q[head++];
//if(head==10000)head=0;
if(!(now.fmr^cow))return now.stp;
if(now.fmr-1>=0&&!vis[now.fmr-1]){
vis[now.fmr-1]=1;
q[tail].fmr=now.fmr-1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
if(now.fmr<=cow&&!vis[now.fmr+1]){
vis[now.fmr+1]=1;
q[tail].fmr=now.fmr+1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
if(now.fmr<=cow&&!vis[now.fmr<<1]){
vis[now.fmr<<1]=1;
q[tail].fmr=now.fmr<<1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&cow)){
memset(q,0,sizeof(que));
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
return 0;
}

poj3278 Catch That Cow的更多相关文章

  1. POJ3278——Catch That Cow(BFS)

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

  2. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  3. POJ3278 Catch That Cow —— BFS

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

  4. POJ3278——Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 114140   Accepted: 35715 ...

  5. POJ3278 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(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

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

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

  8. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

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

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

随机推荐

  1. 服务 {49A27252-A326-4EF1-B698-6EBC7068833C} 的计时器作业 id {573BE459-DF82-481C-84BD-CA14D287450B} 配置刷新的上一个实例仍在运行,因此将跳过当前的实例。请考虑增加作业之间的时间间隔。

    在SharePoint2007的错误日志中发现大量如下错误: 07/02/2013 16:17:25.99     OWSTIMER.EXE (0x0958)     0x097C    Window ...

  2. 查看Android系统是User模式还是Eng模式

    root@itek82_wet_kk:/ # cat default.prop # # ADDITIONAL_DEFAULT_PROPERTIES # ro.secure= ro.allow.mock ...

  3. 【读书笔记】iOS网络-异步请求与运行循环

    异步请求需要运行循环.当数据传递到服务器或是被客户端接收时,运行循环用于实现事件与委托对象之间的通信.异步请求在发出时,会在当前线程的运行循环上操作,这个实现细节是很重要的,因为在GCD块中或者是通过 ...

  4. HTML5 初步了解

    这是HTM5L的第一篇博客. 那么就让我们简单的了解HTML5的语法吧. (HTML5运行环境要求极低,非关键地方就不做截图了) <!--根标签--> <html> <! ...

  5. IOS 手势详解

    在IOS中手势可以让用户有很好的体验,因此我们有必要去了解一下手势. (在设置手势是有很多值得注意的地方) *是需要设置为Yes的点击无法响应* *要把手势添加到所需点击的View,否则无法响应* 手 ...

  6. Safari 快捷键

    标签和网页导航快捷键 8 个 切换到下一个标签页 – Control+Tab 切换到上一个标签页 – Control+Shift+Tab 向下滚动一屏 – 空格 向上滚动一屏 – Shift+空格 焦 ...

  7. SQL Server 2000 sp2 及更低版本不受此版本的 Windows 支持

    SQL Server 2000 sp2 及更低版本不受此版本的 Windows 支持.在安装了 SQL Server 2000 之后请应用 sp3. 出现这种现象的原因在于:Windows Serve ...

  8. hibernate连接数据库和反向工程

    一.JSP界面连接数据库: 导包:将11个包倒进web-inf的lib目录下: 二.建立hibernate.cfg.xml的配置文件:!注意:是放到项目SRC目录下: 三.将视图切换到java下,在左 ...

  9. eclipse常用配置

    一. 手动方式安装svn插件 1. 根据本地的svn客户端的版本,到http://subclipse.tigris.org/ 下载eclipse对应版本的svn插件包 注:装eclipse里面的svn ...

  10. python异常

    Std 异常 异常名 描述 1 Exception 所有的异常 2 StopIteration 迭代器的next()方法不指向任何对象 3 SystemExit sys.exit() 函数执行 4 S ...