【OJ】抓牛问题
/*
农夫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】抓牛问题的更多相关文章
- NOIP模拟赛 抓牛
[题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法 ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- [Swust OJ 781]--牛喝水
Time limit(ms): 1000 Memory limit(kb): 65535 The cows have a line of 20 water bowls from which the ...
- B - Catch That Cow (抓牛)
B - Catch That Cow Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 关于ACM,关于CSU
原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...
- POJ 3278Catch That Cow
http://poj.org/problem?id=3278 大意是说牛在原地不动,他在某点去抓牛,他有两种方式可以走,第一种走一步,往前往后都可,第二种是走现在所在点的两倍的数目.只要能够刚好到达牛 ...
- 9.23 noip模拟试题
Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N ...
- NOIP2014-6-14模拟赛
Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N≤1 ...
- 2017.07.06【NOIP提高组】模拟赛B组
Summary 今天比赛感觉题目很奇葩,都可以用许多简单方法来做,正确性都显然,当然也有点水,也就是说是考我们的数感和数学知识,而程序,只是代码的体现. 这次的时间安排感觉不错,因为很快就打完最后一道 ...
随机推荐
- spring事务源码分析结合mybatis源码(三)
下面将结合mybatis源码来分析下,这种持久化框架是如何对connection使用,来达到spring事务的控制. 想要在把mybatis跟spring整合都需要这样一个jar包:mybatis-s ...
- [mysql]错误解决之"Failed to start MySQL Server"
最近又开始倒腾mysql了,遇到了一个以前没有见过的问题. 问题如下: 百度了好久,发现写的文章都千篇一律,解决办法也都几乎是一样的,然而在我这里一点儿用都没有. 所以FQ看了看外面的世界,终于找到了 ...
- Dos.Common
引言: Dos.Common是一个开发中的常用类库,如HttpHelper.LogHelper.CacheHelper.CookieHelper.MapperHelper等等.与Dos.WeChat. ...
- MUI初学1
1.1)官网: http://www.dcloud.io/ 2)案例: http://www.dcloud.io/case/#group-1 3)文档:http://dev.dcloud.net.cn ...
- github密钥
官网英文资料:https://help.github.com/articles/connecting-to-github-with-ssh/ 1.生成SSH keys文件id_rsa.pub ssh- ...
- table切换jquery插件 jQuery插件写法模板 流程
通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jQuery UI的部件工厂方式创建 通过$.extend()来扩展jQuery $. ...
- BeanPostProcessors (for example: not eligible for auto-proxying),报错解决
最近遇到个问题,springmvc项目debug启动失败 debug启动会卡住不动,run模式启动正常 debug启动输出到下面这行之后,就不会继续输出了 -- :: [INFO]- Bean 'da ...
- Gradle: Download 下载慢的解决办法
应该是gradle被墙了,在网上查得:使用阿里云的国内镜像仓库地址,就可以快速的下载需要的文件: 修改项目根目录下的文件 build.gradle : buildscript { repositori ...
- IntelliJ IDEA 使提示不区分大小写
File ==> Settings ==> Editor ==> General ==> Code Completion 第一行 Match case 将默认勾选去掉
- node爬取html乱码
var http = require('http'), iconv = require('iconv-lite'); http.get("http://website.com/", ...