C# 水排序 微信小游戏

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

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

cs 复制代码
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

输出

相关推荐
懒人咖5 小时前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
bugcome_com6 小时前
深入了解 C# 编程环境及其开发工具
c#
wfserial8 小时前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
阔皮大师9 小时前
INote轻量文本编辑器
java·javascript·python·c#
kylezhao201910 小时前
C# 中的 SOLID 五大设计原则
开发语言·c#
啦啦啦_999911 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
Porco.w11 小时前
C#与三菱PLC FX5U通信
网络·c#
E_ICEBLUE13 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
JQLvopkk15 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
wxin_VXbishe18 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php