DPAlgorithml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:DPAlgorithml
  3. * 作者:xulisong
  4. * 创建时间: 2019/1/31 10:08:02
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using FWindSoft.Data.Graph;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace Test
  15. {
  16. /*
  17. * 动态规划问题的特点,是递推
  18. */
  19. /// <summary>
  20. /// 步数集合
  21. /// </summary>
  22. public class StepSet : List<string>
  23. {
  24. public StepSet()
  25. {
  26. }
  27. public StepSet(List<string> list) : base(list)
  28. {
  29. }
  30. }
  31. public class DPAlgorithml
  32. {
  33. public static List<StepSet> GetStepSet1()
  34. {
  35. return new List<StepSet>() {new StepSet() {"1"}};
  36. }
  37. public static List<StepSet> GetStepSet2()
  38. {
  39. return new List<StepSet>() { new StepSet() { "1","1" },new StepSet(){"2"} };
  40. }
  41. public static List<StepSet> GetSetpSet(int stepNum)
  42. {
  43. var result= new List<StepSet>();
  44. if (stepNum < 0)
  45. return result;
  46. if (stepNum == 1)
  47. {
  48. return GetStepSet1();
  49. }
  50. if (stepNum == 2)
  51. {
  52. return GetStepSet2();
  53. }
  54. var set1= GetSetpSet(stepNum - 1);
  55. var set2 = GetSetpSet(stepNum - 2);
  56. set1.ForEach(s => s.Add("1"));
  57. set2.ForEach(s => s.Add("2"));
  58. result.AddRange(set1);
  59. result.AddRange(set2);
  60. return result;
  61. }
  62. private static List<StepSet> Clone(List<StepSet> sets,string step="1")
  63. {
  64. //集合的映射处理比直接递归还要费时间
  65. //new List<StepSet>(sets);//
  66. return sets.Select(s =>
  67. {
  68. var set= new StepSet(s);
  69. set.Add(step);
  70. return set;
  71. }).ToList();
  72. }
  73. public static List<StepSet> GetSetpSet2(int stepNum)
  74. {
  75. var result = new List<StepSet>();
  76. if (stepNum < 0)
  77. return result;
  78. var backTwo = GetStepSet1();
  79. var backOne = GetStepSet2();
  80. if (stepNum == 1)
  81. {
  82. return backTwo;
  83. }
  84. if (stepNum == 2)
  85. {
  86. return backOne;
  87. }
  88. for (int i = 3; i <= stepNum; i++)
  89. {
  90. var temp = backOne;// new List<StepSet>(backOne);
  91. var set1 = Clone(backOne,"1");
  92. var set2 = backTwo;// Clone(backTwo);
  93. // set1.ForEach(s => s.Add("1"));
  94. set2.ForEach(s => s.Add("2"));
  95. backOne = set1;// new List<StepSet>(set1);
  96. backOne.AddRange(set2);
  97. backTwo = temp;
  98. }
  99. return backOne;
  100. }
  101. }
  102. #region 问题描述
  103. public class DPAlgorithml1
  104. {
  105. #region 描述
  106. /*
  107. * 面值为(1,3,5)的纸币,用最小的张数凑出11
  108. */
  109. #endregion
  110. public static List<string> GetSouluction(List<int> source, int count)
  111. {
  112. var reuslt = CreateSource(source);
  113. return GetSouluction(reuslt,source,count);
  114. }
  115. public static Dictionary<int, List<string>> CreateSource(List<int> source)
  116. {
  117. Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
  118. foreach (var item in source)
  119. {
  120. dic[item] = new List<string>() { item.ToString() };
  121. }
  122. return dic;
  123. }
  124. private static List<string> GetSouluction(Dictionary<int,List<string>> resultCache,List<int> source,int count)
  125. {
  126. if (resultCache.TryGetValue(count,out List<string> re))
  127. {
  128. return new List<string>(re);
  129. }
  130. List<string> result =null;
  131. int useCount = int.MaxValue;
  132. foreach (var step in source)
  133. {
  134. if (count > step)
  135. {
  136. var tempResult = GetSouluction(resultCache,source, count - step);
  137. tempResult.Add(step.ToString());
  138. if (tempResult.Count < useCount)
  139. {
  140. result = tempResult;
  141. useCount = tempResult.Count;
  142. }
  143. }
  144. }
  145. result= new List<string>(result ?? new List<string>());
  146. resultCache[count] = new List<string>(result);
  147. return result;
  148. }
  149. }
  150. #endregion
  151. #region 问题描述
  152. public class DPAlgorithml2
  153. {
  154. #region 描述
  155. /*
  156. *体积价值二维关系(4,7),(2,3),(5,9)
  157. */
  158. #endregion
  159. }
  160. #endregion
  161. public static class TestDijlstra
  162. {
  163. public static string Test()
  164. {
  165. SimpleEdgeGraph graph = new SimpleEdgeGraph();
  166. graph.AddEdge("A", "B", 5);
  167. graph.AddEdge("A", "C", 2);
  168. graph.AddEdge("B", "D", 1);
  169. graph.AddEdge("C", "D", 6);
  170. graph.AddEdge("B", "E", 6);
  171. graph.AddEdge("D", "E", 1);
  172. graph.AddEdge("D", "F", 2);
  173. graph.AddEdge("E", "G", 7);
  174. graph.AddEdge("F", "G", 3);
  175. var results = GraphUtils.Dijkstra(graph, "A");
  176. StringBuilder builder = new StringBuilder();
  177. foreach (var result in results)
  178. {
  179. builder.AppendFormat("path:{0} weight:{1}", string.Join(",", result), result.Weight);
  180. builder.AppendLine();
  181. }
  182. File.WriteAllText(@"d:\dijkstra.txt", builder.ToString());
  183. return builder.ToString();
  184. }
  185. }
  186. public static class TestFloyd
  187. {
  188. public static string Test()
  189. {
  190. SimpleEdgeGraph graph = new SimpleEdgeGraph() { IsDirection = true };
  191. graph.AddEdge("1", "4",4);
  192. graph.AddEdge("1", "2", 2);
  193. graph.AddEdge("4", "3", 12);
  194. graph.AddEdge("4", "1", 5);
  195. graph.AddEdge("3", "4", 1);
  196. graph.AddEdge("3", "1", 17);
  197. graph.AddEdge("2", "3", 3);
  198. graph.AddEdge("1", "3", 6);
  199. var results = GraphUtils.Floyd(graph);
  200. StringBuilder builder = new StringBuilder();
  201. foreach (var result in results)
  202. {
  203. builder.AppendFormat("path:{0} weight:{1}", string.Join(",", result), result.Weight);
  204. builder.AppendLine();
  205. }
  206. File.WriteAllText(@"d:\dijkstra.txt", builder.ToString());
  207. return builder.ToString();
  208. }
  209. }
  210. }