当前位置:   article > 正文

C# 水排序 微信小游戏

C# 水排序 微信小游戏

来只 水排序谜题启发式搜索方法_水排序解法小程序-CSDN博客

大神的C++语言转换成C# 语言,更多的请看原作者,这里直接贴C#代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApp2
  6. {
  7. class Program
  8. {
  9. private const int NC = 12;
  10. private const int SIZE = 4;
  11. private static readonly List<string> vc = new List<string> { "黄色", "绿色", "翠绿", "青色", "蓝色", "紫色", "棕色", "橙色", "红色", "灰蓝", "褐色", "粉色" };
  12. private static List<List<int>> vm = new List<List<int>>();
  13. private static int[] avail = new int[NC];
  14. private static Dictionary<List<List<int>>, int> visit = new Dictionary<List<List<int>>, int>(new ListComparer());
  15. static void Main(string[] args)
  16. {
  17. Init();
  18. Dfs(0);
  19. OutVm();
  20. Console.ReadKey();
  21. }
  22. private static void OutMap()
  23. {
  24. for (int i = 0; i < vc.Count; i++)
  25. {
  26. Console.WriteLine(i + " " + vc[i] + " ");
  27. }
  28. }
  29. private static void Input()
  30. {
  31. int a;
  32. Console.WriteLine("\n输入总试管数目");
  33. a = int.Parse(Console.ReadLine());
  34. vm = new List<List<int>>(new List<int>[a]);
  35. Console.WriteLine("依次输入各试管内颜色,从上往下,空的用-1补足");
  36. for (int i = 0; i < vm.Count; i++)
  37. {
  38. vm[i] = new List<int>();
  39. //for (int j = SIZE - 1; j >= 0; j--)
  40. //{
  41. string xx = Console.ReadLine().ToString();
  42. string[] xxs = xx.Split(' ');
  43. for (int k = 0; k < xxs.Length; k++)
  44. {
  45. a = int.Parse(xxs[k].ToString());
  46. if (a == -1) continue;
  47. vm[i].Insert(0, a);
  48. avail[a]++;
  49. }
  50. //}
  51. }
  52. }
  53. private static bool Check()
  54. {
  55. for (int i = 0; i < NC; i++) if (avail[i] != 0 && avail[i] != SIZE) return false;
  56. return true;
  57. }
  58. private static void Init()
  59. {
  60. OutMap();
  61. do
  62. {
  63. Input();
  64. } while (!Check());
  65. }
  66. private static bool OneCol(List<int> vi)
  67. {
  68. for (int i = 1; i < vi.Count; i++)
  69. {
  70. if (vi[i] != vi[i - 1])
  71. {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. private static bool End()
  78. {
  79. return vm.All(vi => OneCol(vi));
  80. }
  81. private static bool CanPour(int i, int j)
  82. {
  83. if (i == j) return false;
  84. int si = vm[i].Count, sj = vm[j].Count;
  85. if (si == 0 || sj == SIZE) return false;
  86. if (sj == 0)
  87. {
  88. return !OneCol(vm[i]);
  89. }
  90. int ci = vm[i][si - 1], cj = vm[j][sj - 1];
  91. if (ci != cj) return false;
  92. int num = 0;
  93. for (int k = si - 1; k >= 0; k--) if (vm[i][k] == ci) num++;
  94. else break;
  95. return sj + num <= SIZE;
  96. }
  97. private static int Pour(int i, int j)
  98. {
  99. int x = 0;
  100. while (CanPour(i, j))
  101. {
  102. int it = vm[i].Count - 1;
  103. vm[j].Add(vm[i][it]);
  104. vm[i].RemoveAt(it);
  105. x++;
  106. }
  107. return x;
  108. }
  109. private static void PourF(int i, int j, int num)
  110. {
  111. while (num-- > 0)
  112. {
  113. int it = vm[i].Count - 1;
  114. vm[j].Add(vm[i][it]);
  115. vm[i].RemoveAt(it);
  116. }
  117. }
  118. private static bool Dfs(int deep)
  119. {
  120. if (visit.ContainsKey(vm)) return false;
  121. visit[vm] = 1;
  122. if (End() || deep > 40)
  123. {
  124. return true;
  125. }
  126. for (int i = 0; i < vm.Count; i++)
  127. {
  128. for (int j = 0; j < vm.Count; j++)
  129. {
  130. if (!CanPour(i, j)) continue;
  131. int x = Pour(i, j);
  132. if (Dfs(deep + 1))
  133. {
  134. Console.WriteLine("\ndeep = " + deep + " from " + (i + 1) + " to " + (j + 1));
  135. return true;
  136. }
  137. PourF(j, i, x);
  138. }
  139. }
  140. return false;
  141. }
  142. private static void OutVm()
  143. {
  144. Console.WriteLine();
  145. foreach (var vi in vm)
  146. {
  147. int si = vi.Count;
  148. for (int i = SIZE - 1; i >= 0; i--)
  149. {
  150. if (i >= si) Console.Write("-1 ");
  151. else Console.Write(vi[i] + " ");
  152. }
  153. Console.WriteLine();
  154. }
  155. }
  156. //public static void Main()
  157. //{
  158. // Init();
  159. // Dfs(0);
  160. // OutVm();
  161. //}
  162. private class ListComparer : IEqualityComparer<List<List<int>>>
  163. {
  164. public bool Equals(List<List<int>> x, List<List<int>> y)
  165. {
  166. return x.SequenceEqual(y, new SequenceComparer<int>());
  167. }
  168. public int GetHashCode(List<List<int>> obj)
  169. {
  170. int hash = 17;
  171. foreach (var list in obj)
  172. {
  173. foreach (var item in list)
  174. {
  175. hash = hash * 31 + item.GetHashCode();
  176. }
  177. }
  178. return hash;
  179. }
  180. }
  181. private class SequenceComparer<T> : IEqualityComparer<IEnumerable<T>>
  182. {
  183. public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
  184. {
  185. return x.SequenceEqual(y);
  186. }
  187. public int GetHashCode(IEnumerable<T> obj)
  188. {
  189. int hash = 17;
  190. foreach (var item in obj)
  191. {
  192. hash = hash * 31 + item.GetHashCode();
  193. }
  194. return hash;
  195. }
  196. }
  197. }
  198. }

测试

-1 -1 -1 -1
-1 -1 -1 -1
9 5 8 1
3 5 2 9
7 4 2 11
6 8 4 1
3 4 0 11
7 8 2 6
1 10 7 8
6 9 11 2
0 3 10 0
6 10 10 11
9 4 5 3
0 1 5 7

输出

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号