Ejemplo n.º 1
0
        public static bool Solve(SuDokuProblemData data)
        {
            if (data.NumberOfZeroes == 0)
            {
                return(true);
            }

            for (var row = 0; row < 9; ++row)
            {
                for (var col = 0; col < 9; ++col)
                {
                    if (data._data[row][col] == 0)
                    {
                        for (var number = 9; number >= 1; --number)
                        {
                            if (!data.IsInBox(row, col, number) &&
                                !data.IsInRow(row, number) &&
                                !data.IsInColumn(col, number))
                            {
                                data[row, col] = number;
                                if (Solve(data))
                                {
                                    return(true);
                                }
                                data[row, col] = 0;
                            }
                        }

                        return(false);
                    }
                }
            }

            return(data.NumberOfZeroes == 0);
        }
Ejemplo n.º 2
0
/*
 * Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid and its solution grid.
 *
 * A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.
 *
 * The 6K text file, sudoku.txt (right click and 'Save Link/Target As...'), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).
 *
 * By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.
 */

        public static int Problem96()
        {
            var res   = 0;
            var input = File.ReadAllLines(@"Input\sudoku.txt");
            var data  = new SuDokuProblemData();

            for (var s = 0; s < 50; ++s)
            {
                for (var row = 0; row < 9; ++row)
                {
                    var line = input[s * 10 + row + 1];
                    for (var col = 0; col < 9; ++col)
                    {
                        data[row, col] = Int32.Parse(line[col].ToString(CultureInfo.InvariantCulture));
                    }
                }

                Solve(data);
                res += data[0, 0] * 100 + data[0, 1] * 10 + data[0, 2];
            }

            return(res);
        }