码迷,mamicode.com
首页 > 其他好文 > 详细

Catch That Cow

时间:2015-08-06 23:49:54      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

Catch That Cow

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 113   Accepted Submission(s) : 46
Problem 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
 
 1 #include<stdio.h>
 2 #include<queue>
 3 #include<string.h>
 4 using namespace std;
 5 const int MAXN=100010;
 6 int visit[MAXN];
 7 int now[3];
 8 int N,K,step,t;
 9 void bfs(){
10     queue<int>dl;
11     dl.push(N);
12     visit[N]=1;
13     if(N!=K){
14     while(!dl.empty()){
15         t=dl.size();
16         step++;
17         while(t--){
18             now[0]=dl.front()+1;
19             now[1]=dl.front()-1;
20             now[2]=dl.front()*2;
21             dl.pop();
22             for(int i=0;i<3;i++){
23                 if(now[i]==K)return;
24                 if(now[i]>0&&now[i]<=100000&&!visit[now[i]]){//醉了,少了个0,错了n次;;;;; 
25                     visit[now[i]]=1;dl.push(now[i]);
26                 }
27             }
28         }
29     }
30 }
31 }
32 int main(){
33     while(~scanf("%d%d",&N,&K)){
34         memset(visit,0,sizeof(visit));
35         step=0;
36         bfs();
37         printf("%d\n",step);
38     }
39     return 0;
40 }

 

Catch That Cow

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4709312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!