题目描述

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. c++对象模型-对象模型

    1:简单对象模型 1>介绍:每个成员都使用一个指针指向真正的成员.所以对象 的大小很好确定,就是成员数*指针大小. 2>用途:成员函数就是使用这个模型的 3>图: 4>加上继承 ...

  2. LDAP-openldap服务部署和测试(YUM安装)

    1. 概述2. 服务端部署过程2.1 软件包说明2.2 部署过程2.3 配置过程3. 测试4. 生成LDIF格式文件4.1 安装migrationtools工具4.2 用migrationtools生 ...

  3. ExtJs 编译

    前台使用Extjs加载源码的话是非常庞大的,编译之后就只加载一个app.js文件.这种技能如果不知道的话怕别人骂我不是个女程序员.哈哈哈哈哈. 打开cmd,进入程序Extjs的文件夹,如我的程序Ext ...

  4. list 的 增 删

    增: 1. name = [] 2. name.append() 3. name.extend(name2) name2为可迭代的 name + name2 与之效果一样,合并为一个列表 4. nam ...

  5. D. Too Easy Problems

    链接 [http://codeforces.com/group/1EzrFFyOc0/contest/913/problem/D] 题意 给你n个题目,考试时间T,对于每个问题都有一个ai,以及解决所 ...

  6. Neo4j初探

    neo4j-desktop-win64 exe https://neo4j.com/download-thanks-desktop/?edition=desktop&flavour=winst ...

  7. Entity Framwork学习笔记

    一.First Demo

  8. Jquery 组 标签页

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  9. python数据统计量分析

    #-*- coding: utf-8 -*- #餐饮销量数据统计量分析 from __future__ import print_function import pandas as pd cateri ...

  10. linux shell $ 特殊变量

    $0   #Shell本身的文件名 $1-$n   #添加到Shell的各参数值.$1是第1参数.$2是第2参数… $*   #所有参数列表.如"$*"用「"」括起来的情 ...