/// <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 void importTextToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "数独文件(* .txt)|* .txt"; dlg.FilterIndex = 0; dlg.Title = "选择数独文件"; if (dlg.ShowDialog() == DialogResult.OK) { string express = File.ReadAllText(dlg.FileName, Encoding.UTF8); if (express.Length < 81) { return; } currentMarket = new SudokuMarket(StaticTools.StringToList(express)); RefreshPanel(); } }
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); }