Пример #1
0
            static SudokuPuzzle ClearPossiblesForACell(int i, int j, SudokuPuzzle puzzle)
            {
                // 读取一个值,如果此值为0,则继续下一个
                int currVal = puzzle.Item[i][j].Val;

                if (currVal == 0)
                {
                    return(puzzle);
                }
                // 清除根据此值可以排除的值
                // 清除同行的值
                int row = i;
                int col = 0;

                for (col = 0; col < 9; col++)
                {
                    if (col == j)
                    {
                        continue;
                    }

                    SudokuItem tempItem = puzzle.Item[row][col];
                    if (tempItem.Val == currVal)
                    {
                        throw new Exception("同一行有相同值");
                    }


                    if (tempItem.IsConfirmed)
                    {
                        continue;
                    }

                    tempItem.RemoviePossibleVal(currVal);
                }

                // 清除同列的值
                col = j;
                for (row = 0; row < 9; row++)
                {
                    if (row == i)
                    {
                        continue;
                    }

                    SudokuItem tempItem = puzzle.Item[row][col];
                    if (tempItem.Val == currVal)
                    {
                        throw new Exception("同一列有相同值");
                    }


                    if (tempItem.IsConfirmed)
                    {
                        continue;
                    }

                    tempItem.RemoviePossibleVal(currVal);
                }
                // 清除同块的值
                col = j / 3;
                row = i / 3;
                int rowTml = (row + 1) * 3;
                int colTml = (col + 1) * 3;

                for (int tr = row * 3; tr < rowTml; tr++)
                {
                    for (int tc = col * 3; tc < colTml; tc++)
                    {
                        if (tr == i && tc == j)
                        {
                            continue;
                        }

                        SudokuItem tempItem = puzzle.Item[tr][tc];

                        if (tempItem.Val == currVal)
                        {
                            throw new Exception("同一框有相同值");
                        }


                        if (tempItem.IsConfirmed)
                        {
                            continue;
                        }

                        tempItem.RemoviePossibleVal(currVal);
                    }
                }
                return(puzzle);
            }