博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【bfs】USACO抓牛catchcow
阅读量:4696 次
发布时间:2019-06-09

本文共 3163 字,大约阅读时间需要 10 分钟。

这题是黄巨大出的比赛题.

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?

Input

Line 1: Two space-separated integers:
 
N
 and
 
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

Sample Output

4

Hint

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.

Source

 
 
 
 
黄巨大的翻译很不错啊:
 

Problem 1 抓牛(catchcow.cpp/c/pas)

【题目描述】

       农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来.

他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法移动,步行和瞬移:步行每秒种可以让约翰从x处走到x+lx-l处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.

       那么,约翰需要多少时间抓住那只牛呢?

【输入格式】

仅有两个整数NK

【输出格式】

最短时间

【样例输入】

5 17

【样例输出】

4

 

 

思路:1.正常人都想到的bfs,加上适当剪枝就能过

           2.吴桐想的一种两次的dp(多次逼近最优解)

           3.我刚看到题目想的2进制

 

还是先贴正常代码吧:

#include
#include
#include
#include
using namespace std;int d[200005]; //搜寻队列 int f[200005]; //记录每个点最小值 int head,tail,catchmax;int main(){ int n,k,i,j,t; for (i=1;i<=100000;i++) f[i]=100000; cin>>n>>k; if (n>k) {cout<
<
=1)&&(t+1
Run ID User Problem Result Memory Time Language Code Length Submit Time
12976028 Accepted 1268K 16MS 1209B 2014-06-15 16:12:10

下面有正解:似乎跑起来比我的快一点

#include
#include
#include
#define N 100001using namespace std;int n,k,T,q[N],d[N];bool inq[N];void bfs(){ memset(d,127,sizeof(d)); int t=0,w=1; q[0]=n;inq[n]=1;d[n]=0; while(t
d[now]+1) { d[now+1]=d[now]+1; if((now+1)==n)return; if(!inq[now+1]) {inq[now+1]=1;q[w++]=now+1;if(w==N)w=0;} } if(now>0&&d[now-1]>d[now]+1) { d[now-1]=d[now]+1; if((now-1)==n)return; if(!inq[now-1]) {inq[now-1]=1;q[w++]=now-1;if(w==N)w=0;} } if((now<<1)<=T&&d[now<<1]>d[now]+1) { d[now<<1]=d[now]+1; if((now<<1)==n)return; if(!inq[now<<1]) {inq[now<<1]=1;q[w++]=(now<<1);if(w==N)w=0;} } } }int main(){ scanf("%d%d",&n,&k); T=max(n,k)+1; bfs(); printf("%d",d[k]); return 0;}

 

吴桐代码等拿到了贴上来

 

讲讲我写残了不想再写的二进制算法:

          因为可以乘以2,所以用二进制

          先特判终点是否小于起点。。

          将n,k都转成二进制存起来,然后

               while (n的二进制数长度<=k的二进制数长度)

                {

                     将n补成和k前n【0】(n的二进制数长度)位一样的二进制数。

                               补的时候最低位*1;第二位*2;都用一个减另一个,不要大减小,最后abs,这样能避免重复操作

                     如果 (n的二进制数长度<k的二进制数长度) {操作数+1;二进制数长度+1;二进制数最后一位补个0进去即可(*2)}

               }

 

 

看看什么时候心情好重新写吧。

 

 

 

 

 

转载于:https://www.cnblogs.com/seekdreamer/p/3789594.html

你可能感兴趣的文章
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>