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?

    农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上幽发,尽快把那只奶牛抓回来.
    他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有
两种办法移动,步行和瞬移:步行每秒种可以让约翰从z处走到x+l或x-l处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.
    那么,约翰需要多少时间抓住那只牛呢?

Input

* Line 1: Two space-separated integers: N and K

    仅有两个整数N和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
Farmer John starts at point 5 and the fugitive cow is at point 17.

Sample Output

4

OUTPUT DETAILS:

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.

好裸的bfs……n的规模才10w

原来还想的是二进制dp,结果发现我在没事找事……

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m,head,tail=1;
int dist[500001];
int q[500001];
inline bool mark(int x)
{
return !(x<0||x>max(2*m+1,n+1));
}
int main()
{
freopen("catchcow.in","r",stdin);
freopen("catchcow.out","w",stdout);
scanf("%d%d",&n,&m);
memset(dist,-1,sizeof(dist));
q[1]=n;dist[n]=0;
while (head<tail)
{
head++;
int now=q[head]-1;
if(mark(now)&&dist[now]==-1)
{
q[++tail]=now;
dist[now]=dist[q[head]]+1;
}
now=q[head]+1;
if(mark(now)&&dist[now]==-1)
{
q[++tail]=now;
dist[now]=dist[q[head]]+1;
}
now=q[head]*2;
if(mark(now)&&dist[now]==-1)
{
q[++tail]=now;
dist[now]=dist[q[head]]+1;
}
if(dist[m]!=-1) {printf("%d",dist[m]);return 0;}
}
}

  

2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛的更多相关文章

  1. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 634  Solved ...

  2. BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )

    BFS... -------------------------------------------------------------------------------------------- ...

  3. bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】

    满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...

  4. 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这一题开始想到的是dfs啊,,但是本机测样例都已经re了... 那么考虑bfs...很巧妙? ...

  5. 【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++

    题目DescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her ...

  6. 2014.6.14模拟赛【bzoj1592】[Usaco2008 Feb]Making the Grade 路面修整

    Description FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了 ...

  7. 2014.7.8模拟赛【笨笨当粉刷匠】|bzoj1296 [SCOI]粉刷匠

    笨笨太好玩了,农田荒芜了,彩奖用光了,笨笨只好到处找工作,笨笨找到了一份粉刷匠的工作.笨笨有n条木板需要被粉刷.每条木板被分成m个格子,每个格子要被刷成红色或蓝色.笨笨每次粉刷,只能选择一条木板上一段 ...

  8. 东方14模拟赛之noip2015/day1/3/神奇的幻方

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  128000kB 描述 幻方是一种很神奇的N*N 矩阵:它由数字 1,2,3, … …,N*N 构成,且每行.每列及 ...

  9. 2014.7.7 模拟赛【小K的农场】

    3.小K的农场(farm.pas/cpp/c) [题目描述] 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三 ...

随机推荐

  1. Laravel5.2 下使用Form

    laravel到了5.1.*以上版本,便没有了illuminate/html类库的支持, 我试着把illuminate/html类库加入了laravel5.2,依然没有用, 但是laravelcoll ...

  2. java,大数据批量插入、更新

    public void exec(Connection conn){ try { conn.setAutoCommit(false); Long beginTime = System.currentT ...

  3. (转)重置Mac OS X管理员密码

    忘记Mac管理员密码怎么办?别担心,办法总会有的. [方法一] 开机按住option,选择Recovery HD(Snow Leopard插入光盘开机按住C) Snow Leopard系统:进入后在上 ...

  4. 10个利用Eclipse调试Java的常见技巧

    http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...

  5. android 多项对话框

    在main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  6. zend_db连接mysql(附完整代码)(转)

    在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...

  7. Unity用户自定义圆角头像

    前天朋友遇到一个这样的需求,而且比较棘手让我帮忙解决.需求就是棋牌类的游戏,玩家的个人资料中包括自己的头像而且可以浏览相册中的图片或者使用相机拍照设置.关于这个问题我也查阅一些资料,由于涉及安卓部分知 ...

  8. Linux系统调用system_call

    2016-03-25 张超的<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我的虚拟环境和代码在http ...

  9. Ubunte 11.4 下安装 SSH遇到的问题

    第一次安装报了一堆错,主要是也http 404 not found之类的,搜索了一番怀疑是apt的source list的问题. 网上找到一份替换之,我用的是搜狐的服务器.网址如下: http://b ...

  10. 从前有个聊天室(socket&threading)

    服务器端: # -*- coding: utf-8 -*- import socket, threading con = threading.Condition() HOST = raw_input( ...