动态规划【搬运距离】

来源:互联网 发布:安卓防蓝光软件 编辑:程序博客网 时间:2024/05/17 07:01
算法stringclass2010c

 一个骆驮运玉米从A地到B地, 骆驮一次最多运1000个玉米,A地距离B地有1000米远. 而骆驮每走1米就要吃一个玉米.现在有3000个玉米.现在要从A运到B. 问到B地最多还能剩下多少个玉米?

wantalcs给出的答案是:

 

[c-sharp] view plaincopyprint?
  1. class Program  
  2. {  
  3.    private static readonly int _maxLoad = 1000;   //单次运载上限  
  4.     private static readonly int _totals = 3000;    //需要被运载总量  
  5.     private static readonly int _unitDeplete = 1;  //运输单位距离消耗食品量  
  6.     private static readonly int _road = 1000;      //全路长度  
  7.     /// <summary>  
  8.     /// 求本次实际运载量  
  9.     /// </summary>  
  10.     /// <param name="quantity">本次剩余需要运载的量</param>  
  11.     /// <returns>本次实际运载量</returns>  
  12.     private static int GetLoad(int quantity)  
  13.     {  
  14.         return Math.Min(quantity, _maxLoad);  
  15.     }  
  16.     /// <summary>  
  17.     /// 计算运载过程需要消耗的食品量  
  18.     /// </summary>  
  19.     /// <param name="x">运载距离</param>  
  20.     /// <returns>总消耗量</returns>  
  21.     private static int GetDepletion(int x)  
  22.     {  
  23.         return x * _unitDeplete;  
  24.     }  
  25.     /// <summary>  
  26.     /// 单段路程运载结果  
  27.     /// </summary>  
  28.     /// <param name="x">运载的路程</param>  
  29.     /// <param name="quantity">需要被运载的量</param>  
  30.     /// <returns>最后运到目的地的量</returns>  
  31.     private static int PartLoad(int x, int quantity)  
  32.     {  
  33.         int target = 0;         //运到目的地的量  
  34.         int origin = quantity;   //剩余需被运载量  
  35.         target = GetLoad(origin) - GetDepletion(x);  
  36.         origin = origin - GetLoad(origin);  
  37.         while (GetLoad(origin) > GetDepletion(2 * x))   //只要剩余可运量大于来回路程消耗量,就回头去运输  
  38.         {  
  39.             target += (GetLoad(origin) - GetDepletion(2 * x));  
  40.             origin -= (GetLoad(origin));  
  41.         }  
  42.         return target;  
  43.     }  
  44.     /// <summary>  
  45.     /// 计算是指定分段路程下,最终运到目的地的量  
  46.     /// </summary>  
  47.     /// <param name="x">分段路程长度</param>  
  48.     /// <returns>最终到达目的地量</returns>  
  49.     private static int FinalLoad(int x)  
  50.     {  
  51.         int road = _road;       //剩余路程  
  52.         int target = _totals;   //运到的量  
  53.         while (road > x)    //只要剩余路程大于单位路程,就继续运输  
  54.         {  
  55.             road -= x;  
  56.             target = PartLoad(x, target);  
  57.             if (target <= 0)    //如果把所有食品都消耗完了,运输失败,没意义继续下去了。  
  58.             {  
  59.                 break;  
  60.             }  
  61.         }  
  62.         if (target > 0)  
  63.         {  
  64.             target = PartLoad(road, target);    //运输余下最后一段路程  
  65.         }  
  66.         return target > 0 ? target : 0;  
  67.     }  
  68.     static void Main(string[] args)  
  69.     {  
  70.         int unitRoad = 0;   //单位路程   
  71.         int quantity = 0;   //最终运量  
  72.         for (int i = 1; i <= 1000; i++)  
  73.         {  
  74.             int target = FinalLoad(i);  
  75.             if (target > quantity)  
  76.             {  
  77.                 quantity = target;  
  78.                 unitRoad = i;  
  79.             }  
  80.         }  
  81.         Console.WriteLine(string.Format("当单位路程为{0}时,最后运载量最大,可达到{1}。", unitRoad, quantity));  
  82.         Console.ReadKey();  
  83.     }  
  84. }  
 

 

这个问题明显应该使用动态规划算法。目前看来 wantalcs给出的算法是正确的,但我还不是特别的肯定。

0 0