/// <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(""); }
/// <summary> /// 简单随机搜索 /// </summary> /// <param name="sodukuString"></param> /// <returns></returns> private string EasySearch(string sodukuString) { var matrix = StaticTools.StringToList(sodukuString); var locations = StaticTools.GetLocations(matrix); var otherLocations = StaticTools.allLocations.Except(locations).ToList(); Dictionary <string, int> sodukumap = new Dictionary <string, int>(); foreach (var loc in locations) { var newstr = StaticTools.SetZero(sodukuString, loc); var answerCount = new DanceLink().solution_count(newstr); sodukumap.Add(newstr, answerCount); } var tempResult = ""; var index = 1; foreach (var newGene in sodukumap) { //Console.WriteLine("存在多解的提示数 \r\n" + newGene); //Console.WriteLine("进展: " +"处理了"+ index+"条,总数是 "+ sodukumap.Count); index += 1; tempResult = newClues(newGene.Key, otherLocations); } Console.WriteLine("最终提示数表达式为\r\n" + tempResult); return(tempResult); }
/// <summary> /// 将数独指定位置的数进行交换构成标准数独。 /// /// </summary> /// <example> /// new ComfirmedPostion().GenConfirmedPosition(StaticTools.ListToString(sodukuMatrix)); /// </example> /// <param name="sodukuString"></param> public string GenSudoku(string sodukuString, string fileName = "") { var matrix = StaticTools.StringToList(sodukuString); Console.WriteLine(sodukuString); var list = StaticTools.GetLocations(matrix); Console.WriteLine(JsonConvert.SerializeObject(list)); var switchList = PermutationAndCombination <int> .GetCombination(list.ToArray(), 2); //switchList.Reverse(); Console.WriteLine("list" + list.Count); Console.WriteLine("switchList" + switchList.Count); Dictionary <string, int> expressCount = new Dictionary <string, int>(); List <string> tryedList = new List <string> { sodukuString }; var min = GetMinCount(sodukuString, expressCount, switchList); while (min != 1) { var result = (from item1 in expressCount where !(tryedList.Any(item2 => item2 == item1.Key)) select item1).Where(c => c.Value != 0).ToList(); if (result.Count == 0) { //所有该尝试的组合都已经尝试过了 //表明已知提示数在固定位置的确无法构成唯一解。 return(null); } var newSeed = result.OrderBy(c => c.Value).Last(); var newMin = result.OrderBy(c => c.Value).First(); if (true) { Console.WriteLine(fileName + "最少的终盘个数: " + newMin.Value + "表达式为 " + newMin.Key + " 最多的终盘个数: " + newSeed.Value + "表达式为 " + newSeed.Key); } min = GetMinCount(newSeed.Key, expressCount, switchList); tryedList.Add(newSeed.Key); } string Value = expressCount.Where(c => c.Value == 1).Select(c => c.Key).First(); string dir = AppDomain.CurrentDomain.BaseDirectory; var noticeCount = StaticTools.GetLocations(Value).Count; string configName = Path.Combine(dir, fileName + "生成于" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"); File.WriteAllText(configName, Value); return(Value); }
private string newClues(string clues, IEnumerable <int> otherLocations) { var oneAnswer = new DanceLink().GetOneAnswer(clues); //Console.WriteLine("局部结果 " + oneAnswer); var locations = StaticTools.GetLocations(clues); var otherLocationList = otherLocations.ToList(); //Console.WriteLine("剩余元素个数 " + otherLocations.Count()); var exceptList = ExceptOtherPearlGene(otherLocationList, locations, oneAnswer); //Console.WriteLine("不能填数据的位置为 " +JsonConvert.SerializeObject(exceptList)); var pure = otherLocations.Except(exceptList); var result = GetPureGene(oneAnswer, clues, pure); return(result); }
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); }
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); } }
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(""); }
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; }