Пример #1
0
        /// <summary>
        /// 暴力搜索
        /// </summary>
        /// <param name="sodukuString"></param>
        /// <returns></returns>
        private string ForceSearch(string sodukuString)
        {
            var list = StaticTools.GetSubString(sodukuString);

            var index = 1;

            foreach (var subString in list)
            {
                Console.WriteLine("处理字符串   " + index + "  " + subString);
                index += 1;
                var market = new SudokuMarket(subString);
                var result = market.GetCellInfos().Where(c => c.Value.Value == 0).

                             Select(c1 => c1.Value).ToList();
                Dictionary <int, List <int> > locationRest = new Dictionary <int, List <int> >();
                foreach (var cell in result)
                {
                    locationRest.Add(cell.location, cell.initrest);
                }

                var locationcombine = PermutationAndCombination <int> .GetCombination(locationRest.Keys.ToArray(), 2);

                foreach (var combine in locationcombine)
                {
                    var location1 = combine[0];
                    var location2 = combine[1];
                    var rest1     = locationRest[location1];
                    var rest2     = locationRest[location2];

                    foreach (var value1 in rest1)
                    {
                        Dictionary <int, string> one = new Dictionary <int, string> {
                            { location1, "" + value1 }
                        };
                        if (new DanceLink().solution_count(StaticTools.SetValues(sodukuString, one)) > 0)
                        {
                            foreach (var value2 in rest2)
                            {
                                Dictionary <int, string> two = new Dictionary <int, string> {
                                    { location1, "" + value1 }, { location2, "" + value2 }
                                };
                                var result1 = StaticTools.SetValues(sodukuString, two);
                                if (StaticTools.IsPearl(result1))
                                {
                                    string dir         = AppDomain.CurrentDomain.BaseDirectory;
                                    var    noticeCount = StaticTools.GetLocations(result1).Count;
                                    string configName  = Path.Combine(dir, "提示数个数" + noticeCount + "生成于" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");
                                    File.WriteAllText(configName, result1);

                                    Console.WriteLine("当前表达式为:" + result1 + "提示数个数为:" + noticeCount);
                                    return(result1);
                                }
                            }
                        }
                    }
                }
            }

            return("");
        }
Пример #2
0
        private static List <int> ExceptOtherPearlGene(IEnumerable <int> otherLocations, List <int> locations,
                                                       string oneAnswer)
        {
            var exceptList = new List <int>();

            foreach (var loc in otherLocations)
            {
                var newlocations = StaticTools.allLocations.Except(locations.Union(new List <int> {
                    loc
                }));
                var newGens = StaticTools.SetZero(oneAnswer, newlocations);
                if (StaticTools.IsPearl(newGens))
                {
                    exceptList.Add(loc);
                }
            }

            return(exceptList);
        }
Пример #3
0
        private static bool GetNewClues()
        {
            var validString = "300000000000090032095300100200010009060003000050000840030007908600009450980150020";

            Console.WriteLine("输入数据的提示数个数为" + StaticTools.GetLocations(validString).Count);
            var newtest =
                StaticTools.IsPearl(validString);
            var result1 = new SudokuMarket(StaticTools.StringToList(validString)).SubMarkets;

            foreach (var c1 in result1)
            {
                if (StaticTools.IsPearl(c1.StrExpress))
                {
                    Console.WriteLine(c1.StrExpress + "已知数个数为" + StaticTools.GetLocations(c1.StrExpress).Count);
                }
            }


            return(newtest);
        }
Пример #4
0
        public string GetMoreNotice(string sodukuString, int noticeCount)
        {
            if (!StaticTools.IsPearl(sodukuString))
            {
                throw new Exception("输入数独应是应该珍珠题");
            }

            var result  = sodukuString;
            var isbreak = false;

            do
            {
                var tempresult = EasySearch(result);
                if (!string.IsNullOrEmpty(tempresult))
                {
                    result = tempresult;
                }
                else
                {
                    isbreak = true;
                }
                ;
            } while (!isbreak && StaticTools.GetLocations(result).Count < noticeCount);

            if (isbreak)
            {
                Console.WriteLine("暴力搜索开始\r\n");
                do
                {
                    result = ForceSearch(result);
                } while (!string.IsNullOrEmpty(result) && StaticTools.GetLocations(result).Count < noticeCount);

                return(result);
            }
            else
            {
                return(result);
            }
        }
Пример #5
0
        private string GetPureGene(string oneAnswer, string clues, IEnumerable <int> pure)
        {
            var choose = 2;


            var ints = PermutationAndCombination <int> .GetCombination(pure.ToArray(), choose);

            //Console.WriteLine("组合元素个数   " + pure.Count());
            //Console.WriteLine("choose   " + choose);
            //Console.WriteLine("组合个数   " + ints.Count);
            var locations = StaticTools.GetLocations(clues);

            int computedCount = 0;
            int index         = 0;

            foreach (var t in ints)
            {
                var newClues = StaticTools.SetZero(oneAnswer,
                                                   StaticTools.allLocations.Except(t.Union(locations)));
                computedCount += 1;
                if (computedCount == 10000)
                {
                    index        += 1;
                    computedCount = 0;
                    Console.WriteLine("已处理   " + index + "万条数据" + DateTime.Now);
                }

                if (StaticTools.IsPearl(newClues))
                {
                    var result = newClues;

                    return(result);
                }
            }
            return("");
        }
Пример #6
0
        static void Main(string[] args)

        {
            if (1 > 0)
            {
                SudokuMarket sdm =
                    new SudokuMarket(soduku39);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("from sympy import *");
                var cells = sdm.GetCellInfos().Values.ToList();
                foreach (var cell in cells)
                {
                    sb.AppendLine(string.Format("{0}= Symbol('{0}')", cell.variablename));
                }

                sb.AppendLine("print(solve([");

                for (int index = 0; index < 9; index++)
                {
                    var index1 = index;
                    AppendCondition1(cells, c => c.row == index1, sb);
                    AppendCondition1(cells, c => c.column == index1, sb);
                    AppendCondition1(cells, c => c.block == index1, sb);
                    AppendCondition2(cells, c => c.row == index1, sb);
                    AppendCondition2(cells, c => c.column == index1, sb);
                    AppendCondition2(cells, c => c.block == index1, sb);
                }

                var valuedCells = cells.Where(c => c.Value != 0);
                foreach (var cellInfo in valuedCells)
                {
                    sb.AppendLine(string.Format("    {0}{1},", cellInfo.variablename, GetCountValue(cellInfo)));
                }

                sb.AppendLine("     1-1");//消除逗号的影响
                sb.AppendLine(string.Format("],[{0}]))", string.Join(",", cells.Select(c => c.variablename))));


                File.WriteAllText("solveSudoku.py", sb.ToString());
                File.WriteAllText("solveSudoku.begintime.txt", DateTime.Now.ToString());
                Console.WriteLine("开始计算");
                string result = runPython("solveSudoku.py", "");

                File.WriteAllText("solveSudoku.endtime.txt", DateTime.Now.ToString());
                File.WriteAllText("solveSudoku.result", result);
                Console.WriteLine(result);
                Console.WriteLine("运算结束");
                Console.ReadKey();


                var breakin = 0;
            }
            else
            {
                var c1111 = new SudokuMarket(new SudokuBuilder().MakeSudoku()).Pearl.StrExpress;


                var testString =
                    "705602000640305100100000300200000000000000000000004269000000000010040980000098412"; //29个提示数的珍珠盘
                //testString = c1111;
                var test = StaticTools.IsPearl(testString);
                Console.WriteLine("    当前表达式是\r\n" + testString + "\r\n" + StaticTools.GetLocations(testString).Count);
                Console.WriteLine("输入数据的提示数个数为" + StaticTools.GetLocations(testString).Count);
                var c = new MoreClues().GetMoreNotice(testString, 39
                                                      ); //26个提示数

                Console.WriteLine(c);
                Console.ReadKey();
                Console.ReadKey();
                Console.ReadKey();
                Console.ReadKey();
            }


            Console.ReadKey();
            return;
        }