Advertisement
Grimmjow1

Sudoku Solver

Jul 31st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01._3x3problem
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[][] board = new int[9][];
  11.             FillTheBoard(board);  // fill the bord with sudoku numbers
  12.             Solve(0,0, board);
  13.  
  14.         }
  15.  
  16.         private static void Solve(int row ,int col , int[][] board)
  17.         {
  18.             if (col == 9)
  19.             {
  20.                 col = 0;
  21.                 row += 1;
  22.             }
  23.             if (row==9)       // break the recursion.If we reach row outside of the boar.
  24.             {
  25.                 PrintResult(board);  // Print the solve sudoku
  26.                
  27.                 return;
  28.                
  29.             }
  30.             for (int i = row; i < 9; i++)
  31.             {
  32.                 for (int j = col; j < 9; j++)
  33.                 {
  34.                     if (CellisFree(i,j,board))
  35.                     {
  36.                         for (int n = 1; n <= 9; n++)
  37.                         {
  38.                             if (CanPlace(i,j,board,n))   //Checks if we can place n in the free cell.
  39.                             {
  40.                                 board[i][j] = n;
  41.                                 Solve(i, j + 1, board);
  42.                                 Remove(i,j,board);
  43.                             }
  44.                            
  45.                         }
  46.                         return;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         private static void PrintResult(int[][] board)
  53.         {
  54.             for (int i = 0; i < 9; i++)
  55.             {
  56.                 Console.WriteLine( string.Join(" ",board[i]));
  57.             }
  58.         }
  59.  
  60.         private static bool CellisFree(int i, int j, int[][] board)
  61.         {
  62.             if (board[i][j]==0)
  63.             {
  64.                 return true;
  65.             }
  66.             return false;
  67.         }
  68.  
  69.         private static bool CanPlace(int row, int col, int[][] board, int n)
  70.         {
  71.             for (int i = 0; i < 9; i++)
  72.             {
  73.                 if (board[i][col]==n)
  74.                 {
  75.                     return false;
  76.                 }
  77.             }
  78.             for (int i = 0; i < 9; i++)
  79.             {
  80.                 if (board[row][i]==n)
  81.                 {
  82.                     return false;
  83.                 }
  84.             }
  85.  
  86.             return true;
  87.         }
  88.  
  89.         private static void Remove(int i, int j, int[][] board)
  90.         {
  91.             board[i][j] = 0;
  92.         }
  93.  
  94.         private static void FillTheBoard(int[][] board)
  95.         {
  96.             for (int i = 0; i < 9; i++)
  97.             {
  98.                 int[] data = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  99.                 board[i] = data;
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement