SudokuSolver

From JholJhapata
Revision as of 08:13, 1 June 2025 by Admin (talk | contribs) (Created page with "Here’s a simple Sudoku Solver in C# using backtracking. It works on a standard 9x9 Sudoku grid. thumb|SudokuSolver using System; using S...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Here’s a simple Sudoku Solver in C# using backtracking. It works on a standard 9x9 Sudoku grid.


SudokuSolver
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading;
   using System.Threading.Tasks;
   namespace sudoku_console
   {
       internal class Program
       {
           static int SIZE = 9;
           public static void Main()
           {
               int[,] board = {
               {5, 3, 0, 0, 7, 0, 0, 0, 0},
               {6, 0, 0, 1, 9, 5, 0, 0, 0},
               {0, 9, 8, 0, 0, 0, 0, 6, 0},
               {8, 0, 0, 0, 6, 0, 0, 0, 3},
               {4, 0, 0, 8, 0, 3, 0, 0, 1},
               {7, 0, 0, 0, 2, 0, 0, 0, 6},
               {0, 6, 0, 0, 0, 0, 2, 8, 0},
               {0, 0, 0, 4, 1, 9, 0, 0, 5},
               {0, 0, 0, 0, 8, 0, 0, 7, 9}
           };
               if (Solve(board))
               {
                   PrintBoard(board);
                   Console.ReadLine();
               }
               else
               {
                   Console.WriteLine("No solution exists.");
               }
           }
           static bool Solve(int[,] board, bool isTimer = false)
           {
               for (int row = 0; row < SIZE; row++)
               {
                   for (int col = 0; col < SIZE; col++)
                   {
                       if (board[row, col] == 0)
                       {
                           for (int num = 1; num <= 9; num++)
                           {
                               if (IsValid(board, row, col, num))
                               {
                                   board[row, col] = num;
                                   if (isTimer)
                                       Thread.Sleep(1000);
                                   if (Solve(board))
                                       return true;
                                   board[row, col] = 0; // backtrack
                               }
                           }
                           return false; // no valid number found
                       }
                   }
               }
               return true; // board is solved
           }
           static bool IsValid(int[,] board, int row, int col, int num)
           {
               for (int x = 0; x < SIZE; x++)
               {
                   if (board[row, x] == num || board[row, x] == num)
                       return false;
               }
               for (int x = 0; x < SIZE; x++)
               {
                   if (board[x, col] == num || board[x, col] == num)
                       return false;
               }
               int startRow = row / 3 * 3;
               int startCol = col / 3 * 3;
               for (int i = 0; i < 3; i++)
               {
                   for (int j = 0; j < 3; j++)
                   {
                       if (board[startRow + i, startCol + j] == num)
                           return false;
                   }
               }
               return true;
           }
           static void PrintBoard(int[,] board)
           {
               for (int i = 0; i < SIZE; i++)
               {
                   for (int j = 0; j < SIZE; j++)
                   {
                       Console.Write(board[i, j] + " ");
                   }
                   Console.WriteLine();
               }
           }
       }
   }