来只 水排序谜题启发式搜索方法_水排序解法小程序-CSDN博客
大神的C++语言转换成C# 语言,更多的请看原作者,这里直接贴C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApp2
{class Program{private const int NC = 12;private const int SIZE = 4;private static readonly List<string> vc = new List<string> { "黄色", "绿色", "翠绿", "青色", "蓝色", "紫色", "棕色", "橙色", "红色", "灰蓝", "褐色", "粉色" };private static List<List<int>> vm = new List<List<int>>();private static int[] avail = new int[NC];private static Dictionary<List<List<int>>, int> visit = new Dictionary<List<List<int>>, int>(new ListComparer());static void Main(string[] args){Init();Dfs(0);OutVm();Console.ReadKey();}private static void OutMap(){for (int i = 0; i < vc.Count; i++){Console.WriteLine(i + " " + vc[i] + " ");}}private static void Input(){int a;Console.WriteLine("\n输入总试管数目");a = int.Parse(Console.ReadLine());vm = new List<List<int>>(new List<int>[a]);Console.WriteLine("依次输入各试管内颜色,从上往下,空的用-1补足");for (int i = 0; i < vm.Count; i++){vm[i] = new List<int>();//for (int j = SIZE - 1; j >= 0; j--)//{string xx = Console.ReadLine().ToString();string[] xxs = xx.Split(' ');for (int k = 0; k < xxs.Length; k++){a = int.Parse(xxs[k].ToString());if (a == -1) continue;vm[i].Insert(0, a);avail[a]++;}//}}}private static bool Check(){for (int i = 0; i < NC; i++) if (avail[i] != 0 && avail[i] != SIZE) return false;return true;}private static void Init(){OutMap();do{Input();} while (!Check());}private static bool OneCol(List<int> vi){for (int i = 1; i < vi.Count; i++){if (vi[i] != vi[i - 1]){return false;}}return true;}private static bool End(){return vm.All(vi => OneCol(vi));}private static bool CanPour(int i, int j){if (i == j) return false;int si = vm[i].Count, sj = vm[j].Count;if (si == 0 || sj == SIZE) return false;if (sj == 0){return !OneCol(vm[i]);}int ci = vm[i][si - 1], cj = vm[j][sj - 1];if (ci != cj) return false;int num = 0;for (int k = si - 1; k >= 0; k--) if (vm[i][k] == ci) num++;else break;return sj + num <= SIZE;}private static int Pour(int i, int j){int x = 0;while (CanPour(i, j)){int it = vm[i].Count - 1;vm[j].Add(vm[i][it]);vm[i].RemoveAt(it);x++;}return x;}private static void PourF(int i, int j, int num){while (num-- > 0){int it = vm[i].Count - 1;vm[j].Add(vm[i][it]);vm[i].RemoveAt(it);}}private static bool Dfs(int deep){if (visit.ContainsKey(vm)) return false;visit[vm] = 1;if (End() || deep > 40){return true;}for (int i = 0; i < vm.Count; i++){for (int j = 0; j < vm.Count; j++){if (!CanPour(i, j)) continue;int x = Pour(i, j);if (Dfs(deep + 1)){Console.WriteLine("\ndeep = " + deep + " from " + (i + 1) + " to " + (j + 1));return true;}PourF(j, i, x);}}return false;}private static void OutVm(){Console.WriteLine();foreach (var vi in vm){int si = vi.Count;for (int i = SIZE - 1; i >= 0; i--){if (i >= si) Console.Write("-1 ");else Console.Write(vi[i] + " ");}Console.WriteLine();}}//public static void Main()//{// Init();// Dfs(0);// OutVm();//}private class ListComparer : IEqualityComparer<List<List<int>>>{public bool Equals(List<List<int>> x, List<List<int>> y){return x.SequenceEqual(y, new SequenceComparer<int>());}public int GetHashCode(List<List<int>> obj){int hash = 17;foreach (var list in obj){foreach (var item in list){hash = hash * 31 + item.GetHashCode();}}return hash;}}private class SequenceComparer<T> : IEqualityComparer<IEnumerable<T>>{public bool Equals(IEnumerable<T> x, IEnumerable<T> y){return x.SequenceEqual(y);}public int GetHashCode(IEnumerable<T> obj){int hash = 17;foreach (var item in obj){hash = hash * 31 + item.GetHashCode();}return hash;}}}
}
测试
-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
输出