Example #1
0
        private static void TestSolver(StreamWriter writer)
        {
            SudokuSolver   solver;
            List <int[, ]> puzzles;

            string[] lines = File.ReadAllLines(@"../../95hard.txt");

            puzzles = new List <int[, ]>();
            foreach (string line in lines)
            {
                int[,] puzzle = new int[9, 9];
                int i = 0;
                for (int row = 0; row < 9; row++)
                {
                    for (int col = 0; col < 9; col++)
                    {
                        char c = line[i++];
                        if (c == '.')
                        {
                            puzzle[row, col] = 0;
                        }
                        else
                        {
                            puzzle[row, col] = (int)(c - '0');
                        }
                    }
                }
                puzzles.Add(puzzle);
            }

            writer.AutoFlush = false;

            int       idx   = 1;
            long      max   = 0;
            double    avg   = 0.0;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < 1; i++)
            {
                foreach (int[,] puzzle in puzzles)
                {
                    long start = watch.ElapsedMilliseconds;
                    solver        = new SudokuSolver(puzzle);
                    solver.writer = writer;
                    Console.WriteLine("Solving Puzzle {0}", idx);
                    writer.WriteLine("Solving Puzzle {0}", idx++);
                    if (solver.Solve())
                    {
                        long end = watch.ElapsedMilliseconds;
                        Console.WriteLine(" +OK Total: {0}ms (+{1}ms) -> {2} steps", end, end - start, solver.steps);
                        writer.WriteLine(" +OK Total: {0}ms (+{1}ms) -> {2} steps", end, end - start, solver.steps);
                        writer.WriteLine();
                        solver.WritePuzzle(writer);
                        writer.WriteLine();
                        max = ((end - start) > max) ? (end - start) : max;
                        avg = end;
                    }
                    else
                    {
                        writer.WriteLine(" +FAIL");
                    }
                }
            }

            writer.WriteLine("Max: {0}ms", max);
            writer.WriteLine("Avg: {0}ms", avg / 95);

            writer.Flush();
            writer.Close();
        }
Example #2
0
        private static void TestSolver(StreamWriter writer)
        {
            SudokuSolver solver;
            List<int[,]> puzzles;

            string[] lines = File.ReadAllLines(@"../../95hard.txt");

            puzzles = new List<int[,]>();
            foreach (string line in lines)
            {
                int[,] puzzle = new int[9, 9];
                int i = 0;
                for (int row = 0; row < 9; row++)
                {
                    for (int col = 0; col < 9; col++)
                    {
                        char c = line[i++];
                        if (c == '.')
                            puzzle[row, col] = 0;
                        else
                            puzzle[row, col] = (int)(c - '0');
                    }
                }
                puzzles.Add(puzzle);
            }

            writer.AutoFlush = false;

            int idx = 1;
            long max = 0;
            double avg = 0.0;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 1; i++)
            {
                foreach (int[,] puzzle in puzzles)
                {
                    long start = watch.ElapsedMilliseconds;
                    solver = new SudokuSolver(puzzle);
                    solver.writer = writer;
                    Console.WriteLine("Solving Puzzle {0}", idx);
                    writer.WriteLine("Solving Puzzle {0}", idx++);
                    if (solver.Solve())
                    {
                        long end = watch.ElapsedMilliseconds;
                        Console.WriteLine(" +OK Total: {0}ms (+{1}ms) -> {2} steps", end, end - start, solver.steps);
                        writer.WriteLine(" +OK Total: {0}ms (+{1}ms) -> {2} steps", end, end - start, solver.steps);
                        writer.WriteLine();
                        solver.WritePuzzle(writer);
                        writer.WriteLine();
                        max = ((end - start) > max) ? (end - start) : max;
                        avg = end;
                    }
                    else
                        writer.WriteLine(" +FAIL");
                }
            }

            writer.WriteLine("Max: {0}ms", max);
            writer.WriteLine("Avg: {0}ms", avg / 95);

            writer.Flush();
            writer.Close();
        }
Example #3
0
        static void Main(string[] args)
        {
            int[][,] puzzles = new int[][, ]
            {
                // Easy level sudoku (no backtracking)
                new int[, ]
                {
                    { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 6, 4, 8, 5, 0, 0, 3 },
                    { 0, 0, 8, 0, 0, 0, 4, 0, 6 },
                    { 0, 1, 0, 0, 4, 8, 0, 2, 0 },
                    { 2, 0, 0, 7, 3, 0, 5, 0, 4 },
                    { 0, 7, 0, 0, 6, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 4, 0, 0, 0, 0, 0, 2, 7, 8 },
                    { 8, 0, 0, 0, 0, 0, 0, 0, 0 }
                },

                // Mediumn level sudoku
                new int[, ]
                {
                    { 0, 0, 3, 0, 1, 0, 0, 0, 8 },
                    { 6, 8, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 5, 0, 9, 8, 0, 0, 2 },
                    { 0, 0, 1, 0, 0, 0, 0, 9, 7 },
                    { 8, 0, 0, 9, 4, 7, 0, 0, 1 },
                    { 5, 9, 0, 0, 0, 0, 3, 0, 0 },
                    { 7, 0, 0, 8, 5, 0, 9, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 1, 6 },
                    { 1, 0, 0, 0, 3, 0, 7, 0, 0 }
                },

                // Hard level sudoku
                new int[, ]
                {
                    { 0, 0, 9, 1, 0, 0, 0, 0, 0 },
                    { 0, 4, 0, 0, 5, 0, 0, 8, 0 },
                    { 3, 0, 5, 0, 0, 7, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 8, 0, 4, 0 },
                    { 6, 2, 0, 0, 0, 0, 0, 1, 5 },
                    { 0, 1, 0, 3, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 4, 0, 0, 1, 0, 8 },
                    { 0, 8, 0, 0, 9, 0, 0, 3, 0 },
                    { 0, 0, 0, 0, 0, 3, 7, 0, 0 }
                }
            };

            StreamWriter writer = new StreamWriter("3puzzles.txt");

            int idx = 1;

            foreach (int[,] puzzle in puzzles)
            {
                writer.WriteLine("PUZZLE {0}: STEP-BY-STEP SOLVE", idx++);
                writer.WriteLine("{----------------------------------------------------}");
                SudokuSolver solver = new SudokuSolver(puzzle);
                solver.writer = writer;
                solver.Solve();
                writer.WriteLine("{----------------------------------------------------}");
                writer.WriteLine("+OK  SOLVED -> {0} steps", solver.steps);
                solver.WritePuzzle(writer);
                writer.Flush();
                writer.WriteLine();
                writer.WriteLine();
            }

            // if you uncomment this, you may want to comment the step output
            // in SolveRecurse. If you don't your file will be extremely large
            // ~20MB
            //TestSolver(new StreamWriter("95hard_solved.txt"));
        }
Example #4
0
        static void Main(string[] args)
        {
            int[][,] puzzles = new int[][,]
            {   
                // Easy level sudoku (no backtracking)
                new int[,]
                {
                    {0,0,0,0,1,0,0,0,0},
                    {0,0,6,4,8,5,0,0,3},
                    {0,0,8,0,0,0,4,0,6},
                    {0,1,0,0,4,8,0,2,0},
                    {2,0,0,7,3,0,5,0,4},
                    {0,7,0,0,6,0,0,1,0},
                    {0,0,0,0,0,0,0,0,0},
                    {4,0,0,0,0,0,2,7,8},
                    {8,0,0,0,0,0,0,0,0} 
                },

                // Mediumn level sudoku
                new int[,]
                {
                    {0,0,3,0,1,0,0,0,8},
                    {6,8,0,0,0,0,0,0,0},
                    {0,0,5,0,9,8,0,0,2},
                    {0,0,1,0,0,0,0,9,7},
                    {8,0,0,9,4,7,0,0,1},
                    {5,9,0,0,0,0,3,0,0},
                    {7,0,0,8,5,0,9,0,0},
                    {0,0,0,0,0,0,0,1,6},
                    {1,0,0,0,3,0,7,0,0} 
                },

                // Hard level sudoku
                new int[,]
                {
                    {0,0,9,1,0,0,0,0,0},
                    {0,4,0,0,5,0,0,8,0},
                    {3,0,5,0,0,7,0,0,0},
                    {0,0,0,0,0,8,0,4,0},
                    {6,2,0,0,0,0,0,1,5},
                    {0,1,0,3,0,0,0,0,0},
                    {0,0,0,4,0,0,1,0,8},
                    {0,8,0,0,9,0,0,3,0},
                    {0,0,0,0,0,3,7,0,0} 
                }
            };

            StreamWriter writer = new StreamWriter("3puzzles.txt");

            int idx = 1;
            foreach (int[,] puzzle in puzzles)
            {
                writer.WriteLine("PUZZLE {0}: STEP-BY-STEP SOLVE", idx++);
                writer.WriteLine("{----------------------------------------------------}");
                SudokuSolver solver = new SudokuSolver(puzzle);
                solver.writer = writer;
                solver.Solve();
                writer.WriteLine("{----------------------------------------------------}");
                writer.WriteLine("+OK  SOLVED -> {0} steps", solver.steps);
                solver.WritePuzzle(writer);
                writer.Flush();
                writer.WriteLine();
                writer.WriteLine();
            }

            // if you uncomment this, you may want to comment the step output
            // in SolveRecurse. If you don't your file will be extremely large
            // ~20MB
            //TestSolver(new StreamWriter("95hard_solved.txt"));
        }