Sudoku Solver

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

 

namespace SudokuSolver

{

    class Program

    {

        static void Main(string[] args)

        {

            int[,] sudoku = new int[9, 9];

            int nonZeroCount = 0;

            //Getting the element inputs from user

            for (int i = 0; i < 9; i++)

            {

                for (int j = 0; j < 9; j++)

                {

                    Console.Write("Enter Element (" + (i + 1) + "," + (j + 1) + "): ");

                    sudoku[i, j] = int.Parse(Console.ReadLine().ToString());

                    if (sudoku[i, j] != 0)

                    {

                        nonZeroCount++;

                    }

                }

            }

            Console.WriteLine("Unsolved Sudoku: ");

            for (int i = 0; i < 9; i++)

            {

                for (int j = 0; j < 9; j++)

                {

                    Console.Write(sudoku[i, j] + " ");

                }

                Console.WriteLine();

            }

            Console.WriteLine();

            Stopwatch watch = new Stopwatch();

            watch.Start();

            while (nonZeroCount != (9 * 9))

            {

                for (int i = 0; i < 9; i++)

                {

                    for (int j = 0; j < 9; j++)

                    {

                        if (sudoku[i, j] == 0)

                        {

                            if (Solve(sudoku, i, j))

                            {

                                nonZeroCount++;

                            }

                        }

                    }

                }

            }

            //Printing the solved sudoku

            Console.WriteLine("Solved Sudoku: ");

            for (int i = 0; i < 9; i++)

            {

                for (int j = 0; j < 9; j++)

                {

                    Console.Write(sudoku[i, j] + " ");

                }

                Console.WriteLine();

            }

            watch.Stop();

            Console.WriteLine("Elapsed: {0}", watch.Elapsed);

        }

        public static bool Solve(int[,] sudoku, int row, int col)

        {

            int[] check = new int[9];

            for (int i = 0; i < 9; i++)

            {

                check[i] = 0;

            }

            //Row Check

            for (int j = 0; j < 9; j++)

            {

                if (sudoku[row, j] != 0)

                {

                    check[sudoku[row, j] – 1] = 1;

                }

            }

            //Column Check

            for (int i = 0; i < 9; i++)

            {

                if (sudoku[i, col] != 0)

                {

                    check[sudoku[i, col] – 1] = 1;

                }

            }

            int rowStart = 0;

            int rowEnd = 0;

            int colStart = 0;

            int colEnd = 0;

            //Sub Matrix Check

            if (row < 3)

            {

                rowStart = 0;

                rowEnd = 3;

            }

            else if (row < 6)

            {

                rowStart = 3;

                rowEnd = 6;

            }

            else

            {

                rowStart = 6;

                rowEnd = 9;

            }

            if (col < 3)

            {

                colStart = 0;

                colEnd = 3;

            }

            else if (col < 6)

            {

                colStart = 3;

                colEnd = 6;

            }

            else

            {

                colStart = 6;

                colEnd = 9;

            }

            for (int i = rowStart; i < rowEnd; i++)

            {

                for (int j = colStart; j < colEnd; j++)

                {

                    if (sudoku[i, j] != 0)

                    {

                        check[sudoku[i, j] – 1] = 1;

                    }

                }

            }

            //Check for the matrix

            int count = 0, flag = 0;

            for (int i = 0; i < check.Length; i++)

            {

                if (check[i] == 0)

                {

                    count++;

                    flag = i;

                }

            }

            if (count == 1)

            {

                sudoku[row, col] = flag + 1;

                return true;

            }

            return false;

        }

    }

}

 

Enter Element (1,1): 9
Enter Element (1,2): 0
Enter Element (1,3): 5
Enter Element (1,4): 0
Enter Element (1,5): 0
Enter Element (1,6): 0
Enter Element (1,7): 0
Enter Element (1,8): 0
Enter Element (1,9): 8
Enter Element (2,1): 4
Enter Element (2,2): 0
Enter Element (2,3): 0
Enter Element (2,4): 5
Enter Element (2,5): 7
Enter Element (2,6): 0
Enter Element (2,7): 1
Enter Element (2,8): 0
Enter Element (2,9): 6
Enter Element (3,1): 0
Enter Element (3,2): 2
Enter Element (3,3): 7
Enter Element (3,4): 6
Enter Element (3,5): 0
Enter Element (3,6): 0
Enter Element (3,7): 0
Enter Element (3,8): 4
Enter Element (3,9): 0
Enter Element (4,1): 0
Enter Element (4,2): 9
Enter Element (4,3): 6
Enter Element (4,4): 0
Enter Element (4,5): 0
Enter Element (4,6): 3
Enter Element (4,7): 5
Enter Element (4,8): 1
Enter Element (4,9): 2
Enter Element (5,1): 7
Enter Element (5,2): 0
Enter Element (5,3): 4
Enter Element (5,4): 0
Enter Element (5,5): 1
Enter Element (5,6): 0
Enter Element (5,7): 3
Enter Element (5,8): 0
Enter Element (5,9): 0
Enter Element (6,1): 2
Enter Element (6,2): 1
Enter Element (6,3): 0
Enter Element (6,4): 9
Enter Element (6,5): 8
Enter Element (6,6): 0
Enter Element (6,7): 0
Enter Element (6,8): 0
Enter Element (6,9): 4
Enter Element (7,1): 0
Enter Element (7,2): 8
Enter Element (7,3): 1
Enter Element (7,4): 0
Enter Element (7,5): 0
Enter Element (7,6): 4
Enter Element (7,7): 0
Enter Element (7,8): 9
Enter Element (7,9): 0
Enter Element (8,1): 3
Enter Element (8,2): 0
Enter Element (8,3): 0
Enter Element (8,4): 8
Enter Element (8,5): 0
Enter Element (8,6): 0
Enter Element (8,7): 0
Enter Element (8,8): 5
Enter Element (8,9): 1
Enter Element (9,1): 0
Enter Element (9,2): 0
Enter Element (9,3): 2
Enter Element (9,4): 0
Enter Element (9,5): 0
Enter Element (9,6): 7
Enter Element (9,7): 0
Enter Element (9,8): 6
Enter Element (9,9): 0
Unsolved Sudoku:
9 0 5 0 0 0 0 0 8
4 0 0 5 7 0 1 0 6
0 2 7 6 0 0 0 4 0
0 9 6 0 0 3 5 1 2
7 0 4 0 1 0 3 0 0
2 1 0 9 8 0 0 0 4
0 8 1 0 0 4 0 9 0
3 0 0 8 0 0 0 5 1
0 0 2 0 0 7 0 6 0

 

Solved Sudoku:
9 6 5 4 2 1 7 3 8
4 3 8 5 7 9 1 2 6
1 2 7 6 3 8 9 4 5
8 9 6 7 4 3 5 1 2
7 5 4 2 1 6 3 8 9
2 1 3 9 8 5 6 7 4
6 8 1 3 5 4 2 9 7
3 7 9 8 6 2 4 5 1
5 4 2 1 9 7 8 6 3
Elapsed: 00:00:00.0181858
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s