题目描述

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?

题目描述

每次牛都可以在数轴上移动,每次+1或者-1,或者是坐标*2,求最少的步数。

解法

非常非常非常地容易就可以知道这道题肯定是宽搜宽搜宽搜LJ附身,那么我们按照bfs一步一步来就可以了。

虽然有一些比较杂的优化,但是大体不变就可以A掉了。

ac代码

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#define N 200005
using namespace std;
struct node{int x,st;};
bool vis[N];
queue<node>q;
int main(){
    int x,y;
    while(~scanf("%d%d",&x,&y)){
        while(!q.empty())q.pop();
        memset(vis,false,sizeof(vis)); vis[x]=true;
        q.push((node){x,0});
        while(!q.empty()){
            node u=q.front(); q.pop();
            if(u.x==y){printf("%d\n",u.st);break;}
            int ste=u.st+1;
            if(u.x+1<=100000&&!vis[u.x+1])q.push((node){u.x+1,ste}),vis[u.x+1]=true;
            if(u.x-1>=0&&!vis[u.x-1])q.push((node){u.x-1,ste}),vis[u.x-1]=true;
            if(u.x*2<=100000&&!vis[u.x*2])q.push((node){u.x*2,ste}),vis[u.x*2]=true;
        }
    }
    return 0;
}

[poj3278]抓住那头牛的更多相关文章

  1. POJ-3278 抓住这头牛

    广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if    else if,这样的话就直走一条路了. #include <ios ...

  2. noi 2971 抓住那头牛

    2971:抓住那头牛 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N ...

  3. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  4. OpenJudge 2971 抓住那头牛

    总时间限制:  2000ms 内存限制:  65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0< ...

  5. noi.openjudge——2971 抓住那头牛

    http://noi.openjudge.cn/ch0205/2971/ 总时间限制:  2000ms 内存限制:  65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫 ...

  6. OpenJudge 4001:抓住那头牛

    题目链接 题解: 这个题可以用广搜来解决,从农夫到牛的走法每次都有三种选择,定义一个队列,把农夫的节点加进队列,然后以这三种走法找牛,队列先进先出,按顺序直到找到牛的位置. 代码: #include& ...

  7. 双向广搜 codevs 3060 抓住那头奶牛

    codevs 3060 抓住那头奶牛 USACO  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold   题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...

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

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

  9. 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

随机推荐

  1. [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

    UWP的UI主要由布局容器和内容控件(ContentControl)组成.布局容器是指Grid.StackPanel等继承自Panel,可以拥有多个子元素的类.与此相对,ContentControl则 ...

  2. 虚拟机console基础环境配置——系统镜像站点配置

    1. 概述2. 部署HTTP服务器2.1 YUM安装httpd2.2 配置httpd2.3 启动httpdf2.4 测试httpd3. 部署FTP服务器3.1 YUM安装vsftpd3.2 配置vsf ...

  3. SpringMvc返回Json调试

    spring-web-5.0.6.RELEASE.jar!/org/springframework/web/method/support/HandlerMethodReturnValueHandler ...

  4. Chrome F12调试

    F12 断点 F5 Esc 选择鼠标右击:编辑||hover样式 Sonrces go to file (ctrl+p) 搜搜文件名

  5. Struts2中的图片验证码

    1.Struts中建一个action <action name="Code" class="LoginAction" method="code& ...

  6. css3-弹性盒模型

    first <style> .box{width:1024px;height:100px; border:5px solid black; padding:10px; display:-w ...

  7. Windows samba history

    https://blogs.technet.microsoft.com/josebda/2013/10/02/windows-server-2012-r2-which-version-of-the-s ...

  8. js选择排序。

    <script> , , , , , , , ]; ; j<len; j++ ){ // 假设min为最小值 var minIndex = j; var min = arr[j]; ...

  9. mybatis批量插入和批量更新

    批量插入数据使用的sql语句是: insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.xml的代码如下: & ...

  10. [转帖]Nginx 的 TCP 负载均衡介绍

    Nginx 的 TCP 负载均衡介绍 https://www.cnblogs.com/felixzh/ 前几天同事问 nginx的代理 当时以为只有http的 现在看起来还有tcp的可以使用tcp 代 ...