Grimmjow1

Sudoku Solver 1.1

Aug 1st, 2019
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 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.                 Console.WriteLine();
  27.                 Console.ReadLine();
  28.                 return;
  29.  
  30.             }
  31.             for (int i = row; i < 9; i++)
  32.             {
  33.                 for (int j = 0; j < 9; j++)
  34.                 {
  35.                     if (CellisFree(i, j, board))
  36.                     {
  37.                         for (int n = 1; n <= 9; n++)
  38.                         {
  39.                             if (CanPlace(i, j, board, n))   //Checks if we can place n in the free cell.
  40.                             {
  41.                                 board[i][j] = n;
  42.                                 //PrintResult(board);
  43.                                // Console.ReadLine();
  44.                                 Solve(i, j +1, board);
  45.                                 Remove(i, j, board);
  46.                             }
  47.  
  48.                         }
  49.                         return;
  50.                     }
  51.                 }
  52.                 if (row==8)
  53.                 {
  54.                     Console.WriteLine("Result!!!");
  55.                     PrintResult(board);  // Print the solve sudoku
  56.                     Console.ReadLine();
  57.                     return;
  58.                 }
  59.             }
  60.         }
  61.  
  62.         private static void PrintResult(int[][] board)
  63.         {
  64.             for (int i = 0; i < 9; i++)
  65.             {
  66.                 Console.WriteLine(string.Join(" ", board[i]));
  67.             }
  68.         }
  69.  
  70.         private static bool CellisFree(int i, int j, int[][] board)
  71.         {
  72.             if (board[i][j] == 0)
  73.             {
  74.                 return true;
  75.             }
  76.             return false;
  77.         }
  78.  
  79.         private static bool CanPlace(int row, int col, int[][] board, int n)
  80.         {
  81.            
  82.             for (int i = 0; i < 9; i++)
  83.             {
  84.                 if (board[i][col] == n)
  85.                 {
  86.                     return false;
  87.                 }
  88.             }
  89.             for (int i = 0; i < 9; i++)
  90.             {
  91.                 if (board[row][i] == n)
  92.                 {
  93.                     return false;
  94.                 }
  95.             }
  96.             if (row <= 2 && col <= 2)
  97.             {
  98.                 bool a = CheckSmallSquare(0, 3, 0, 3, board,n);
  99.                 return a;
  100.             }
  101.             else if (row <= 2 && col >= 3 && col <= 5)
  102.             {
  103.                 bool a =  CheckSmallSquare(0, 3, 3, 6, board,n);
  104.                 return a;
  105.             }
  106.             else if (row <= 2 && col >= 6 && col <= 8)
  107.             {
  108.                 bool a = CheckSmallSquare(0, 3, 6, 9, board, n);
  109.                 return a;
  110.             }
  111.             else if (row >= 3 && row <= 5 && col <= 2)
  112.             {
  113.                 bool a = CheckSmallSquare(3, 6, 0, 3, board, n);
  114.                 return a;
  115.             }
  116.             else if (row >= 3 && row <= 5 && col >= 3 && col <= 5)
  117.             {
  118.                 bool a = CheckSmallSquare(3, 6, 3, 6, board, n);
  119.                 return a;
  120.             }
  121.             else if (row >= 3 && row <= 5 && col >= 6 && col <= 8)
  122.             {
  123.                 bool a = CheckSmallSquare(3, 6, 6, 9, board, n);
  124.                 return a;
  125.             }
  126.             else if (row >= 6 && row <= 8 && col <= 2)
  127.             {
  128.                 bool a = CheckSmallSquare(6, 9, 0, 3, board, n);
  129.                 return a;
  130.             }
  131.             else if (row >= 6 && row <= 8 && col >= 3 && col <= 5)
  132.             {
  133.                 bool a = CheckSmallSquare(6, 9, 3, 6, board, n);
  134.                 return a;
  135.             }
  136.             else if (row >= 6 && row <= 8 && col >= 6 && col <= 8)
  137.             {
  138.                 bool a = CheckSmallSquare(6, 9, 6, 9, board, n);
  139.                 return a;
  140.             }
  141.  
  142.             return true;
  143.         }
  144.  
  145.         private static bool CheckSmallSquare(int startI, int endI, int startJ, int endJ, int[][] board, int number)
  146.         {
  147.             for (int i = startI; i < endI; i++)
  148.             {
  149.                 for (int j   = startJ; j < endJ; j++)
  150.                 {
  151.                     if (board[i][j]==number)
  152.                     {
  153.                         return false;
  154.                     }
  155.                 }
  156.             }
  157.             return true;
  158.         }
  159.  
  160.         private static void Remove(int i, int j, int[][] board)
  161.         {
  162.             board[i][j] = 0;
  163.         }
  164.  
  165.         private static void FillTheBoard(int[][] board)
  166.         {
  167.             for (int i = 0; i < 9; i++)
  168.             {
  169.                 int[] data = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  170.                 board[i] = data;
  171.             }
  172.         }
  173.     }
  174. }
Add Comment
Please, Sign In to add comment