[ 登录注册 ]

语言

USACO抓牛catchcow (bfs)

2017-07-14 10:27:52 admin 返回上一页

标签:des   style   class   blog   code   http   

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

http://poj.org/problem?id=3278

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 ≤ 100000) on a number line and the cow is at a point K (0 ≤ K ≤ 100000) 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

USACO 2007 Open Silver
 
 
 
 
黄巨大的翻译很不错啊:
 

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<iostream>#include<cstdlib>#include<cstdio>#include<cstring>using namespace std;int d[200005];             //搜寻队列 int f[200005];             //记录每个点最小值 int headtailcatchmax;int main(){int nkijt;for (i=1;i<=100000;i++) f[i]=100000;cin>>n>>k;if (n>k) {cout<<n-k<<endl; return 0;}  //就怕丧心病狂倒着走的出题人  tail=1;head=1;d[tail]=n;f[n]=0;f[k]=k-n;                       //正着走最坏情况就是一步一步走过去,所以最大只会到k-n  while (head<=tail){t=f[d[head]];catchmax=f[k];             //当前最优解 if (f[d[head]]<catchmax)   //比当前最优解大当然不用找了          {if ((d[head]*2<=2*k+1)&&(t+1<f[d[head]*2]))     //不能大于k的两倍+1,可以证明那样一定不会是最优解            {f[d[head]*2]=t+1;d[tail+1]=d[head]*2;tail++;}if ((d[head]+1<=2*k+1)&&(t+1<f[d[head]+1])){f[d[head]+1]=t+1;d[tail+1]=d[head]+1;tail++;}if ((d[head]+1<=2*k+1)&&(d[head]-1>=1)&&(t+1<f[d[head]-1])){f[d[head]-1]=t+1;d[tail+1]=d[head]-1;tail++;}}head++;       //当时我把这句话放到前一个if判断中去死循环了。。因为如果到某个点等于当前最优解就进不了if判断也退不出while循环     }cout<<f[k]<<endl;return 0;}
Run IDUserProblemResultMemoryTimeLanguageCode LengthSubmit Time
12976028seekdreamer3278Accepted1268K16MSC++1209B2014-06-15 16:12:10

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

#include<iostream>#include<cstdio>#include<cstring>#define N 100001using namespace std;int nkTq[N]d[N];bool inq[N];void bfs(){memset(d127sizeof(d));int t=0w=1;q[0]=n;inq[n]=1;d[n]=0;while(t<w){int now=q[t];t++;if(t==N)t=0;if(now<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;}}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(nk)+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)}

               }

 

 

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

 

 

 

 

 

USACO抓牛catchcow (bfs)布布扣bubuko.com

USACO抓牛catchcow (bfs)

标签:des   style   class   blog   code   http   


文章来源:http://www.bozhiyue.com/yuyan/2017/0714/1484260.html
返回上一页    返回分类 上一篇:   下一篇:
相关