捉住那头牛

来源:互联网 发布:组态软件学哪个 编辑:程序博客网 时间:2024/05/01 15:04
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 32679 Accepted: 10060

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 - 1 or + 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


题意:

          FJ要抓奶牛。

         开始输入N(FJ的位置)K(奶牛的位置)。

         FJ有三种移动方法:1、向前走一步,耗时一分钟。

                                              2、向后走一步,耗时一分钟。

                                             3、向前移动到当前位置的两倍N*2,耗时一分钟。

       问FJ抓到奶牛的最少时间。PS:奶牛是不会动的。

 

思路:1、如果FJ不在奶牛后面,那么他只有一步步往后移动到奶牛位置了,即N>=K时,输出N-K即可。

          2、否则bfs+队列查找(具体见下面的分析&&代码区)

相关算法:

            1、STL中的队列。(PS:周四才在数据结构上了解bfs的真正思想,惭愧啊!)

                需要的头文件:STL是C++中的 #include<iostream>

                                                                 using namespace std;

                                         queue队列容器的头文件 #include<queue>

               queue队列的相关用法:先进先出(FIFO)

                                                     入队push()   //即插入元素

                                                     出队pop()    //即删除元素

                                                     front()        //读取队首元素

                                                     back()       //读取队尾元素

                                                     empty()     //判断队列是否为空

                                                     size()        //读取队列当前元素的个数         

              2、bfs思想:节点进行广度优先搜索的顺序。

                                                                         

                               搜索实现方法(非递归):

                               算法思想:1.设置一个队列Q,从顶点出发,遍历该顶点后让其进队;

                                                 2.出队一个顶点元素,求该顶点的所有邻接点(对应于此题即FJ的三种走法),

                                                    对于没有遍历过的邻接点遍历之,并 让其进队;

                                                 3.若队空停止,队不空时继续第2步。

                             关于bfs数据结构思想的详细介绍:http://zh.wikipedia.org/wiki/BFS


看了牛人博客后的代码,总算了解bfs了,Orz

代码一:C++STL&&bfs版本:

  1. //Accepted  984K    79MS    C++ 1128B   2012-11-10 00:44:26  
  2. #include<iostream>  
  3. #include<queue>  
  4. #include<cstring>  
  5. #include<cstdio>  
  6. using namespace std;  
  7.   
  8. const int maxn=100001;  
  9.   
  10. bool vis[maxn];//标记数组  
  11. int step[maxn];//记录到了每一位置所走的步数  
  12. queue <int> q;//定义队列  
  13.   
  14. int bfs(int n,int k)  
  15. {  
  16.     int head,next;  
  17.     q.push(n);   //开始FJ在n位置,n入队  
  18.     step[n]=0;  
  19.     vis[n]=true//标记已访问  
  20.     while(!q.empty())  //当队列非空  
  21.     {  
  22.         head=q.front();  //取队首  
  23.         q.pop();         //弹出对首  
  24.         for(int i=0;i<3;i++)     //FJ的三种走法  
  25.         {  
  26.             if(i==0) next=head-1;  
  27.             else if(i==1) next=head+1;  
  28.             else next=head*2;  
  29.             if(next<0 || next>=maxn) continue//排除出界情况  
  30.             if(!vis[next])  //如果next位置未被访问  
  31.             {  
  32.                 q.push(next);    //入队  
  33.                 step[next]=step[head]+1;  //步数+1  
  34.                 vis[next]=true;  //标记已访问  
  35.             }  
  36.             if(next==k) return step[next];  //当遍历到结果,返回步数  
  37.         }  
  38.     }  
  39. }  
  40. int main()  
  41. {  
  42.     int n,k;  
  43.     while(cin>>n>>k)  
  44.     {  
  45.         memset(step,0,sizeof(step));  
  46.         memset(vis,false,sizeof(vis));  
  47.         if(n>=k) printf("%d\n",n-k);  
  48.         else printf("%d\n",bfs(n,k));  
  49.     }  
  50.     return 0;  
  51. }  


原创粉丝点击