public void InitCells() { int x = 0; int location = 0; foreach (var lists in initValues) { int y = 0; foreach (var value in lists) { CellInfo cell = new CellInfo(x, y); cell.currentMarket = this; cell.InitValue(value); cellInfos.Add("postion_" + x + "_" + y, cell); blockCells[cell.block].Add(cell); columnCells[cell.column].Add(cell); rowCells[cell.row].Add(cell); locationCellDic.Add(location, cell); location += 1; y += 1; } x += 1; } }
/// <summary> /// 解题 /// </summary> /// <returns></returns> public void Solve(List <List <int> > values, bool firsttime) { //SudokuMarket.rowDatas = FilledDatas(); //SudokuMarket.columnDatas = FilledDatas(); //SudokuMarket.blockDatas = FilledDatas(); cellInfos = new Dictionary <string, CellInfo>(); int row = 0; foreach (var list in values) { int column = 0; foreach (var value in list) { CellInfo cell = new CellInfo(row, column); if (value != 0) { cell.InitValue(value); } cellInfos.Add("postion_" + row + "_" + column, cell); blockCells[cell.block].Add(cell); rowCells[cell.row].Add(cell); columnCells[cell.column].Add(cell); column += 1; } row += 1; } bool fillflag = true; int round = 1; while (fillflag) { fillflag = false; var weiyu = cellInfos.Values.Where(c => c.Value == 0 && c.GetRest().Count == 1).ToList(); foreach (var cell in weiyu) { var value = cell.GetRest()[0]; SolveMessage += "\r\n第" + round + "轮唯余法:" + (cell.row + 1) + "行" + (cell.column + 1) + "列的值为" + value + "\r\n"; cell.SetValue(value); fillflag = true; } SolveMessage += "\r\n"; foreach (var blockCell in blockCells) { fillflag |= GetSingleValue(round, blockCells, blockCell.Key, "宫摒除"); } foreach (var blockCell in columnCells) { fillflag |= GetSingleValue(round, columnCells, blockCell.Key, "列摒除"); } foreach (var blockCell in rowCells) { fillflag |= GetSingleValue(round, rowCells, blockCell.Key, "行摒除"); } for (int i = 1; i < 9; i++) { //针对行的xwing var result = GetXwing(i, GetCellsDic(MethodDiction.Row)); GetRowXwing(result, round, i); //针对列的xwing var result1 = GetXwing(i, GetCellsDic(MethodDiction.Column)); GetColumnXwing(result1, round, i); } for (int index = 0; index < 9; index++) { HiddenTriplet(MethodDiction.Row, index); HiddenTriplet(MethodDiction.Column, index); HiddenTriplet(MethodDiction.Block, index); blockSingleRow(MethodDiction.Block, index); blockSingleColomn(MethodDiction.Block, index); } var tryurlist = cellInfos.Values.Where(c => c.Value == 0 && c.isTwoValue == true).ToList(); foreach (var cellInfo in tryurlist) { test(cellInfo); Uniquerectangle(cellInfo); } foreach (var VARIABLE in cellInfos.Values.Where(c => c.Value == 0)) { test111(VARIABLE); } round = round + 1; } foreach (var kv in rowCells) { var temp = kv.Value.Where(c => c.Value == 0); foreach (var cellInfo in temp) { SolveMessage += cellInfo.ProgramPostion + JsonConvert.SerializeObject(cellInfo.GetRest()) + "\r\n"; } SolveMessage += "\r\n"; } }