/*
农夫John的奶牛跑路了。将地图视作一条数轴,John的初始位置在s而奶牛的位置在t(0<=s,t<=100000)。John可以花费一分钟的时间使自己作如下移动: 1 从点x移动到点x+1
2 从点x移动到点x-1
3 从点x移动到点x*2
奶牛的位置一直在点t。现在给定s,t,要求John要追上奶牛最少需要几分钟。 Sample Input:
5 17 Sample Output:
4 Description:
5->4->8->16->17
*/ #include<iostream>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
const int M=; int bfs(int s,int t)
{
queue<int> q;
int head,next;
int vis[M],step[M];
memset(vis,,sizeof(vis));
memset(step,,sizeof(step));
q.push(s);
step[s]=; while(!q.empty())
{
head=q.front();
q.pop();
for(int i=;i<;i++)
{
if(i==)
next=head-;
if(i==)
next=head+;
if(i==)
next=head*;
if(next<||next>M)
continue;
if(vis[next]==)
{
vis[next]++;
step[next]=step[head]+;
q.push(next);
}
if(next==t)
return step[next];
}
}
} int main()
{
int s,t;
while(scanf("%d %d",&s,&t)==&&s>=&&t<=M)
{
if(t<=s)//牛在人后面
cout<<abs(s-t)<<endl;
else
cout<<bfs(s,t)<<endl;
} return ;
} /*
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn=100001; bool vis[maxn];//标记数组
int step[maxn];//记录到了每一位置所走的步数
queue <int> q;//定义队列 int bfs(int n,int k)
{
int head,next;
q.push(n); //开始FJ在n位置,n入队
step[n]=0;
vis[n]=true; //标记已访问
while(!q.empty()) //当队列非空
{
head=q.front(); //取队首
q.pop(); //弹出对首
for(int i=0;i<3;i++) //FJ的三种走法
{
if(i==0) next=head-1;
else if(i==1) next=head+1;
else next=head*2;
if(next<0 || next>=maxn) continue; //排除出界情况
if(!vis[next]) //如果next位置未被访问
{
q.push(next); //入队
step[next]=step[head]+1; //步数+1
vis[next]=true; //标记已访问
}
if(next==k) return step[next]; //当遍历到结果,返回步数
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,false,sizeof(vis)); while(!q.empty()) q.pop(); //注意调用前要先清空
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
}
return 0;
}
*/ /*
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include <algorithm>
using namespace std; queue<int> q;
int m,n;
int step[100010];
int vis[100010];
int rear,front;//rear表示下一步
int BFS(){
int i;
q.push(n);//把农民的位置压入队列
step[n]=0;//步数记为0
vis[n]=1;////标记这个点走过
while(!q.empty()){//队列不为空哦时执行
front=q.front();//最开始的位置
q.pop();//弹出队列头
for(i=0;i<3;i++)//三种走法,三种循环
{
if(i==0)
rear=front+1;//第一种下一步+1
if(i==1)
rear=front-1;//第二种下一步-1
if(i==2)
rear=front*2;//第三种步数翻倍
if(rear>=0&&rear<=100000&&vis[rear]==0)//判断是否越界,并且这一步没有走过
{
vis[rear]=1;//标记这一步走过了
step[rear]=step[front]+1;// 步数+1
q.push(rear);//将当前位置压入队列
}
if(rear==m)return step[rear];
}
}return -1; } int main()
{ cin>>n>>m;
memset(step,0,sizeof(step));//初始化为0
memset(vis,0,sizeof(vis));//初始化为false
cout<<BFS(); return 0;
}
*/

tz@HZAU

2019/3/4

【OJ】抓牛问题的更多相关文章

  1. NOIP模拟赛 抓牛

    [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法 ...

  2. ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)

    这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...

  3. [Swust OJ 781]--牛喝水

    Time limit(ms): 1000 Memory limit(kb): 65535   The cows have a line of 20 water bowls from which the ...

  4. B - Catch That Cow (抓牛)

    B - Catch That Cow Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. 关于ACM,关于CSU

    原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...

  6. POJ 3278Catch That Cow

    http://poj.org/problem?id=3278 大意是说牛在原地不动,他在某点去抓牛,他有两种方式可以走,第一种走一步,往前往后都可,第二种是走现在所在点的两倍的数目.只要能够刚好到达牛 ...

  7. 9.23 noip模拟试题

      Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N ...

  8. NOIP2014-6-14模拟赛

    Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤1 ...

  9. 2017.07.06【NOIP提高组】模拟赛B组

    Summary 今天比赛感觉题目很奇葩,都可以用许多简单方法来做,正确性都显然,当然也有点水,也就是说是考我们的数感和数学知识,而程序,只是代码的体现. 这次的时间安排感觉不错,因为很快就打完最后一道 ...

随机推荐

  1. 【Java编程思想笔记】注解--元注解

    参考文章:(小白的小小白的白 )https://blog.csdn.net/weixin_42315600/article/details/80630669 https://www.cnblogs.c ...

  2. 题解-CodeChef IOPC14L Sweets Problem

    Problem CodeChef-IOPC14L 题目概要:给定 \(n\) 种糖果且给定每种糖果的数量 \(A_i\),\(Q\) 组询问,每次问选出 \(S\) 个糖果的方案数(模\(10^9+7 ...

  3. CVE_2012_1876堆溢出分析

    首先用windbg附加进程ie页面内容进程,!gflag +hpa添加堆尾检查,.childdbg 1允许子进程调试,然后加载POC. POC: <html> <body> & ...

  4. 【原创】大数据基础之ORC(1)简介

    https://orc.apache.org Optimized Row Columnar (ORC) file 行列混合存储 层次结构: file -> stripes -> row g ...

  5. hdu3555数位dp基础

    /* dp[i][0|1|2]:没有49的个数|最高位是9,没有49的个数|有49的个数 dp[i][0]=10*dp[i-1][0]-dp[i-1][1] dp[i][1]=dp[i-1][0] d ...

  6. cmd下,regsvr32不是内部或外部命令

    https://jingyan.baidu.com/article/48b37f8d2fb1aa1a646488cc.html

  7. 金蝶k3wise 核算项目、辅助资料

    金蝶k3wise 核算项目.辅助资料 ----核算项目信息 select * from t_ItemClass ----核算项目字段信息 --锁定字段 ----核算项目内容表 select * fro ...

  8. Java集合实现

    set: public class BSTSet<E extends Comparable<E>> implements Set<E> { private BST& ...

  9. 模块度Q

    模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...

  10. Berlekamp_Massey 算法 (BM算法) 学习笔记

    原文链接www.cnblogs.com/zhouzhendong/p/Berlekamp-Massey.html 前言 BM算法用于求解常系数线性递推式. 它可以在 $O(n^2)$ 的时间复杂度内解 ...