/// <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(""); }
private void GenWholeToolStripMenuItem_Click(object sender, EventArgs e) { var result = sdkBuilder.MakeSudoku(); currentMarket = new SudokuMarket(result); RefreshPanel(); }
public void Solve(SudokuMarket market) { var tempquestion = new List <List <int> >(); StringBuilder sb = new StringBuilder(); Console.WriteLine(sb.ToString()); }
private void button1_Click_1(object sender, EventArgs e) { foreach (var variable in TextBoxdic) { variable.Value.Text = null; } currentMarket = currentMarket.LessNoticeNumber(); RefreshPanel(); }
public void AddMarket(SudokuMarket market, int newNoticeCount) { if (marketList.ContainsKey(newNoticeCount)) { marketList[newNoticeCount].Add(market); } else { marketList.Add(newNoticeCount, new List <SudokuMarket> { market }); } minValue = Math.Min(minValue, newNoticeCount); }
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); }
/// <summary> /// 生成初盘提示数个数为noticeValue的具有唯一解的初盘 /// </summary> /// <param name="wholeSudoku">完整数独</param> /// <param name="noticeCounts">提示数个数</param> /// <returns></returns> public SudokuMarket AutoQuestion(List <List <int> > wholeSudoku, int noticeCounts) { var tempValue = 0; bool flag = true; var RandomValue = 30; if (noticeCounts >= 81) { noticeCounts = 80; } if (noticeCounts < 17) { noticeCounts = 17; } List <int> locations = new List <int>(); List <List <int> > result; if (noticeCounts >= RandomValue) { #region 多余30个提示数 直接采取随机数生成。 return(SubAutoQuestion(wholeSudoku, noticeCounts, ref locations)); #endregion } else { bool flag1 = true; int trytimes = 0; SudokuMarket market = null; List <SudokuMarket> markets = new List <SudokuMarket>(); while (flag1) { market = SubAutoQuestion(wholeSudoku, RandomValue, ref locations); markets.Add(market); result = market.initValues; var needlist = GetAllNeedList(result, locations); if (needlist.Count > noticeCounts) { } else if (needlist.Count == noticeCounts) { var valueCopy = JsonConvert.DeserializeObject <List <List <int> > >(JsonConvert.SerializeObject(result)); StaticTools.InitQuestion(needlist, valueCopy); if (StaticTools.IsVaildSudoku(valueCopy)) { return(new SudokuMarket(valueCopy)); } } else { var chooseValue = noticeCounts - needlist.Count; var values = locations.Except(needlist).ToArray(); List <int[]> listCombination = PermutationAndCombination <int> .GetCombination(values, chooseValue); int index = 0; foreach (var items in listCombination) { var c = items.ToList(); var location1 = needlist.Union(c).ToList(); var valueCopy = JsonConvert.DeserializeObject <List <List <int> > >(JsonConvert.SerializeObject(result)); StaticTools.InitQuestion(location1, valueCopy); if (StaticTools.ValidNoticeList(location1)) { if (StaticTools.IsVaildSudoku(valueCopy)) { return(new SudokuMarket(valueCopy)); } } index += 1; } } trytimes += 1; if (trytimes == 3) { flag1 = false; } } return(markets.OrderBy(c => c.difficult).Last()); } }
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; }
private void NormalToolStripMenuItem_Click(object sender, EventArgs e) { bool istest = true; if (!istest) { currentMarket = sdkGenerator.AutoQuestion(sdkBuilder.MakeSudoku(), int.Parse(noticeNumber.Text)); } else { List <List <int> > param = new List <List <int> >() { new List <int> { 7, 0, 5, 6, 0, 0, 8, 0, 4 }, new List <int> { 6, 4, 0, 0, 0, 0, 0, 2, 7 }, new List <int> { 1, 2, 8, 4, 7, 0, 0, 5, 6 }, new List <int> { 2, 5, 1, 0, 6, 0, 0, 0, 8 }, new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new List <int> { 8, 0, 0, 0, 5, 0, 2, 6, 0 }, new List <int> { 0, 8, 0, 0, 3, 0, 0, 7, 0 }, new List <int> { 5, 0, 2, 7, 4, 0, 0, 8, 3 }, new List <int> { 3, 0, 7, 5, 0, 0, 4, 0, 2 } }; currentMarket = new SudokuMarket(param); } questions = currentMarket.initValues; UpdateCurrentSudokuInfo(); showhelp = this.ShowHelpToolStripMenuItem.Checked; for (int i = 0; i < questions.Count; i++) { var list = questions[i]; for (int j = 0; j < list.Count; j++) { var value = list[j]; TextBox testBox = TextBoxdic["postion_" + i + "_" + j]; if (testBox == null) { continue; } testBox.Text = "" + value; testBox.ForeColor = _config.ColorConfig.QuestionForeColor; testBox.BackColor = Color.White; resultMessage.Text = null; } } RefreshPanel(); var breakouot = 0; }