zoukankan      html  css  js  c++  java
  • 【原】 POJ 3278 Catch That Cow BFS单源无权图最短距离 解题报告

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

    方法:
    单源无权图最短距离,即BFS
    该问题只需要求得某两点间的最短距离,所以不必求得所有节点的最短距离,一旦处理了目的地点,即可返回结果

    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.

       1: #include <stdio.h>
       2: #include <iostream>
       3: #include <vector>
       4: #include <queue>
       5:  
       6: using namespace std ;
       7:  
       8: typedef vector<int> Table ;
       9: const int INF = 0x7fffffff ;
      10: const int MaxRange = 100000 ;
      11:  
      12: void run3278()
      13: {
      14:     int i ;
      15:     int n,k ;
      16:     int range ;
      17:     int adjArr[3] ;
      18:     int vertex,adV ;
      19:     int curDist ;
      20:     queue<int> Q ;
      21:  
      22:     scanf("%d%d", &n,&k) ;
      23:  
      24:     //这里容易出错:Table开得太小。
      25:     //题意中已经给出最大的节点标号,所以按此数初始化Table
      26:     Table T( MaxRange+1,INF ) ;  //初始化table有MaxRange个无穷值
      27:     
      28:     //BFS
      29:     T[n] = 0 ;  //起始点距离为0
      30:     Q.push(n) ;
      31:     while( !Q.empty() )
      32:     {
      33:         vertex = Q.front() ;
      34:         Q.pop() ;
      35:         curDist = T[vertex] ;
      36:  
      37:         //得到结果
      38:         if( vertex == k )
      39:         {
      40:             printf( "%d\n" , T[vertex] ) ;
      41:             return ;
      42:         }
      43:  
      44:         //***得到节点v的邻接点,可能是v-1、v+1、2*v
      45:         //超出范围的设为-1
      46:         if( vertex-1<0 )
      47:             adjArr[0] = -1 ;
      48:         else
      49:             adjArr[0] = vertex-1 ;
      50:  
      51:         if( vertex+1>MaxRange )
      52:             adjArr[1] = -1 ;
      53:         else
      54:             adjArr[1] = vertex+1 ;
      55:  
      56:         if( vertex*2>MaxRange )
      57:             adjArr[2] = -1 ;
      58:         else
      59:             adjArr[2] = vertex*2 ;
      60:         //***
      61:  
      62:         //对未经处理的邻接点进行处理
      63:         for( i=0 ; i<3 ; ++i )
      64:         {
      65:             if( ( adV=adjArr[i] ) == -1 )
      66:                 continue ;
      67:             if( T[adV] == INF )
      68:             {
      69:                 T[adV] = curDist+1 ;
      70:  
      71:                 //得到结果
      72:                 if( adV == k )
      73:                 {
      74:                     printf( "%d\n" , T[adV] ) ;
      75:                     return ;
      76:                 }
      77:  
      78:                 Q.push(adV) ;
      79:             }
      80:         }
      81:     }
      82: }

    如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

    原创文章属于Allen Sun
    欢迎转载,但请注明文章作者Allen Sun和链接
  • 相关阅读:
    spring data jpa 分页查询(小结)
    java 工作日判断
    Map的merge方法(java8)
    java8 常用JVM 参数修改
    ubuntu docker client 安装
    scala io 读写文件
    Grafana 系统资源监测
    Java8Stream Collectors收集器
    EhCacheUtils 缓存 ehche (将文件临时保存在磁盘)
    springboot2.X 整合scala
  • 原文地址:https://www.cnblogs.com/allensun/p/1872072.html
Copyright © 2011-2022 走看看