Exemple #1
0
        public void test(CellInfo cellInfo)
        {
            var cellsDic     = GetCellsDic(MethodDiction.Row);
            var cellsDic2    = GetCellsDic(MethodDiction.Block);
            var relatedCell  = xywing(cellsDic[cellInfo.row], cellInfo);
            var relatedCell2 = xywing(cellsDic[cellInfo.block], cellInfo);

            if (relatedCell != null && relatedCell2 != null && relatedCell2.block != relatedCell.block)
            {
                SolveMessage += "row cellInfo" + cellInfo.ProgramPostion + "  " + cellInfo.RestInfo + "   " +
                                "relatedCell  " + relatedCell.ProgramPostion + "  " + relatedCell.RestInfo +
                                "   relatedCell2   " + relatedCell2.ProgramPostion + "  " + relatedCell2.RestInfo +
                                "\r\n";
            }

            relatedCell = xywing(cellsDic[cellInfo.column], cellInfo);
            if (relatedCell != null && relatedCell2 != null && relatedCell2.block != relatedCell.block)
            {
                SolveMessage += " column cellInfo" + cellInfo.ProgramPostion + "  " + cellInfo.RestInfo + "   " +
                                "relatedCell  " + relatedCell.ProgramPostion + "  " + relatedCell.RestInfo +
                                "   relatedCell2   " + relatedCell2.ProgramPostion + "  " + relatedCell2.RestInfo +
                                "\r\n";
            }
        }
Exemple #2
0
 public bool Equals(CellInfo x)
 {
     return(ProgramPostion == x.ProgramPostion);
 }
Exemple #3
0
        /// <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";
            }
        }
Exemple #4
0
        /// <summary>
        /// x,y单元格同列,元素a属于其中一个;
        /// y,z单元格同宫,a属于其中一个;
        /// 若x,z不同宫,不同列;
        /// 则x,y所在列位置s第三宫
        /// 若存在元素b使得z所在列元素第三个宫的值为a且x也为a。则b是位置s的待删除项。
        /// </summary>
        public void test111(CellInfo cellInfo)
        {
            var column = cellInfo.column;
            var block  = cellInfo.block;
            var row    = cellInfo.row;
            var y      = cellInfos["postion_" + row + "_" + column];
            var rests  = y.GetRest();

            foreach (var value in rests)
            {
                var z    = OtherPostion(y, MethodDiction.Block, block, value);
                var x    = OtherPostion(y, MethodDiction.Column, column, value);
                var r    = OtherPostion(y, MethodDiction.Row, row, value);
                var flag = false;

                if (x != null && z != null && x.GetRest().Count == 2)
                {
                    var xrest = x.GetRest();
                    var left  = xrest.First(c => c != value);
                    if (z.block != x.block &&
                        z.column != x.column)
                    {
                        var blockid   = BlocksInfo.GetLeftBlock(z.block, x.block);
                        var blocklist = blockCells[blockid];
                        var templist  = blocklist.Where(cell => cell.RestInfo == x.RestInfo && cell.column == z.column).ToList();;
                        if (templist.Count == 1)
                        {
                            foreach (var variable in blocklist)
                            {
                                if (variable.column == column)
                                {
                                    flag = true;
                                    variable.Conflict.Add(left);
                                }
                            }
                            foreach (var variable in GetCellsDic(MethodDiction.Block)[x.block])
                            {
                                if (variable.column == z.column)
                                {
                                    flag = true;
                                    variable.Conflict.Add(left);
                                }
                            }

                            if (flag)
                            {
                                break;
                            }
                        }
                    }
                }

                if (r != null && z != null && r.GetRest().Count == 2)
                {
                    var xrest = r.GetRest();
                    var left  = xrest.First(c => c != value);
                    if (z.block != r.block &&
                        z.column != r.row)
                    {
                        var blockid   = BlocksInfo.GetLeftBlock(z.block, r.block);
                        var blocklist = blockCells[blockid];
                        var templist  = blocklist.Where(cell => cell.RestInfo == r.RestInfo && cell.row == z.row).ToList();;
                        if (templist.Count == 1)
                        {
                            foreach (var variable in blocklist)
                            {
                                if (variable.row == row)
                                {
                                    flag = true;
                                    variable.Conflict.Add(left);
                                }
                            }
                            foreach (var variable in GetCellsDic(MethodDiction.Block)[r.block])
                            {
                                if (variable.column == z.row)
                                {
                                    flag = true;
                                    variable.Conflict.Add(left);
                                }
                            }

                            if (flag)
                            {
                                break;
                            }
                        }
                    }
                }
            }



            var break1 = 1;
        }
Exemple #5
0
        /// <summary>
        /// 唯一矩阵
        /// </summary>
        private void Uniquerectangle(CellInfo cellInfo)
        {
            var row    = cellInfo.row;
            var column = cellInfo.column;

            var a = cellInfo;


            CellInfo other1 = PairNumbers(cellInfo, MethodDiction.Column, column);

            CellInfo other2 = PairNumbers(cellInfo, MethodDiction.Row, row);

            if (other1 != null && other2 != null)
            {
                var location = "postion_" + other1.row + "_" + other2.column;
                var cell     = cellInfos[location];

                List <int> blocks = new List <int>();
                if (!blocks.Contains(a.block))
                {
                    blocks.Add(a.block);
                }

                if (!blocks.Contains(other1.block))
                {
                    blocks.Add(other1.block);
                }

                if (!blocks.Contains(other2.block))
                {
                    blocks.Add(other2.block);
                }

                if (!blocks.Contains(cell.block))
                {
                    blocks.Add(cell.block);
                }

                if (blocks.Count != 2)
                {
                    return;
                }

                var c = cell.GetRest();
                if (c.Count == 3)
                {
                    var restlist = a.GetRest();
                    var flag     = true;
                    foreach (var i in restlist)
                    {
                        if (c.Contains(i))
                        {
                            continue;
                        }
                        flag = false;
                        break;
                    }

                    if (flag)
                    {
                        cell.UrList = restlist;
                    }
                }
            }
        }