C# 水排序 微信小游戏

02-27 1394阅读

来只 水排序谜题启发式搜索方法_水排序解法小程序-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 vc = new List { "黄色", "绿色", "翠绿", "青色", "蓝色", "紫色", "棕色", "橙色", "红色", "灰蓝", "褐色", "粉色" };
        private static List vm = new List();
        private static int[] avail = new int[NC];
        private static Dictionary visit = new Dictionary(new ListComparer());
        static void Main(string[] args)
        {
            Init();
            Dfs(0);
            OutVm();
            Console.ReadKey();
        }
        private static void OutMap()
        {
            for (int i = 0; i = 0; j--)
                //{
                    string xx = Console.ReadLine().ToString();
                    string[] xxs = xx.Split(' ');
                    for (int k = 0; k  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  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 = 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
        {
            public bool Equals(List x, List y)
            {
                return x.SequenceEqual(y, new SequenceComparer());
            }
            public int GetHashCode(List obj)
            {
                int hash = 17;
                foreach (var list in obj)
                {
                    foreach (var item in list)
                    {
                        hash = hash * 31 + item.GetHashCode();
                    }
                }
                return hash;
            }
        }
        private class SequenceComparer : IEqualityComparer
        {
            public bool Equals(IEnumerable x, IEnumerable y)
            {
                return x.SequenceEqual(y);
            }
            public int GetHashCode(IEnumerable obj)
            {
                int hash = 17;
                foreach (var item in obj)
                {
                    hash = hash * 31 + item.GetHashCode();
                }
                return hash;
            }
        }
    }
}

测试

C# 水排序 微信小游戏

-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

输出

C# 水排序 微信小游戏

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]