Advertisement
APXOHT

Sudoku

Jan 27th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.88 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4.  
  5. class SudokuSolve
  6. {
  7.     static int[,] Sudoku = new int[9, 9];
  8.     static bool[,] canBeChanged = new bool[9, 9];
  9.  
  10.     static void PrintSudoku()
  11.     {
  12.         Console.SetCursorPosition(0, 0);
  13.         for (int i = 0; i < 9; i++)
  14.         {
  15.             for (int j = 0; j < 9; j++)
  16.             {
  17.                 if (Sudoku[i, j] == 0)
  18.                 {
  19.                     Console.Write('-');
  20.                 }
  21.                 else
  22.                 {
  23.                     Console.Write(Sudoku[i,j]);
  24.                 }
  25.             }
  26.             Console.WriteLine();
  27.         }
  28.     }
  29.     /*
  30.         53--7----
  31.         6--195---
  32.         -98----6-
  33.         8---6---3
  34.         4--8-3--1
  35.         7---2---6
  36.         -6----28-
  37.         ---419--5
  38.         ----8--79
  39.      */
  40.  
  41.     static void SudokuSolver(int row, int col)
  42.     {
  43.         Thread.Sleep(20);
  44.         PrintSudoku();
  45.         if (row == 8 && col == 8)
  46.         {
  47.             if (Sudoku[row, col] != 0)
  48.             {
  49.                 throw new Exception();
  50.             }
  51.             else
  52.             {
  53.                 for (int i = 1; i <= 9; i++)
  54.                 {
  55.                     if (CheckRow(row, i))
  56.                     {
  57.                         continue;
  58.                     }
  59.                     if (CheckColumn(col, i))
  60.                     {
  61.                         continue;
  62.                     }
  63.                     if (CheckSubSquare(row, col, i))
  64.                     {
  65.                         continue;
  66.                     }
  67.  
  68.                     Sudoku[row, col] = i;
  69.  
  70.                     throw new Exception();
  71.                 }
  72.             }
  73.         }
  74.  
  75.         if (Sudoku[row, col] == 0)
  76.         {
  77.             for (int i = 1; i <= 9; i++)
  78.             {
  79.                 if (CheckRow(row, i))
  80.                 {
  81.                     continue;
  82.                 }
  83.                 if (CheckColumn(col, i))
  84.                 {
  85.                     continue;
  86.                 }
  87.                 if (CheckSubSquare(row, col, i))
  88.                 {
  89.                     continue;
  90.                 }
  91.  
  92.                 Sudoku[row, col] = i;
  93.  
  94.                 SudokuSolver(NextRow(row, col), NextCol(row, col));
  95.             }
  96.         }
  97.         else
  98.         {
  99.             SudokuSolver(NextRow(row, col), NextCol(row, col));
  100.         }
  101.  
  102.         if (canBeChanged[row, col])
  103.         {
  104.             Sudoku[row, col] = 0;
  105.         }
  106.     }
  107.    
  108.     static int NextRow(int row, int col)
  109.     {
  110.         col++;
  111.         if (col > 8)
  112.         {
  113.             row++;
  114.         }
  115.  
  116.         return row;
  117.     }
  118.  
  119.     static int NextCol(int row, int col)
  120.     {
  121.         col++;
  122.         if (col > 8)
  123.         {
  124.             col = 0;
  125.         }
  126.  
  127.         return col;
  128.     }
  129.  
  130.     static bool CheckRow(int row, int number)
  131.     {
  132.         bool hasNumberInRow = false;
  133.  
  134.         for (int i = 0; i < 9; i++)
  135.         {
  136.             if (Sudoku[row, i] == number)
  137.             {
  138.                 hasNumberInRow = true;
  139.                 break;
  140.             }
  141.         }
  142.  
  143.         return hasNumberInRow;
  144.     }
  145.  
  146.     static bool CheckColumn(int column, int number)
  147.     {
  148.         bool hasNumberInColumn = false;
  149.  
  150.         for (int i = 0; i < 9; i++)
  151.         {
  152.             if (Sudoku[i, column] == number)
  153.             {
  154.                 hasNumberInColumn = true;
  155.                 break;
  156.             }
  157.         }
  158.  
  159.         return hasNumberInColumn;
  160.     }
  161.  
  162.     static bool CheckSubSquare(int row, int col, int number)
  163.     {
  164.         bool hasNumberInSquare = false;
  165.         int startRow = 0;
  166.         int startCol = 0;
  167.  
  168.         if (row < 3)
  169.         {
  170.             if (col < 3)
  171.             {
  172.                 startRow = 0;
  173.                 startCol = 0;
  174.             }
  175.             else if (col < 6)
  176.             {
  177.                 startRow = 0;
  178.                 startCol = 3;
  179.             }
  180.             else
  181.             {
  182.                 startRow = 0;
  183.                 startCol = 6;
  184.             }
  185.         }
  186.         else if (row < 6)
  187.         {
  188.             if (col < 3)
  189.             {
  190.                 startRow = 3;
  191.                 startCol = 0;
  192.             }
  193.             else if (col < 6)
  194.             {
  195.                 startRow = 3;
  196.                 startCol = 3;
  197.             }
  198.             else
  199.             {
  200.                 startRow = 3;
  201.                 startCol = 6;
  202.             }
  203.         }
  204.         else
  205.         {
  206.             if (col < 3)
  207.             {
  208.                 startRow = 6;
  209.                 startCol = 0;
  210.             }
  211.             else if (col < 6)
  212.             {
  213.                 startRow = 6;
  214.                 startCol = 3;
  215.             }
  216.             else
  217.             {
  218.                 startRow = 6;
  219.                 startCol = 6;
  220.             }
  221.         }
  222.  
  223.  
  224.         for (int i = startRow; i < startRow + 3; i++)
  225.         {
  226.             for (int j = startCol; j < startCol + 3; j++)
  227.             {
  228.                 if (Sudoku[i, j] == number)
  229.                 {
  230.                     hasNumberInSquare = true;
  231.                     return hasNumberInSquare;
  232.                 }
  233.             }
  234.         }
  235.  
  236.         return hasNumberInSquare;
  237.     }
  238.  
  239.     static void Main()
  240.     {
  241.         for (int i = 0; i < 9; i++)
  242.         {
  243.             string input = Console.ReadLine();
  244.  
  245.             for (int j = 0; j < input.Length; j++)
  246.             {
  247.                 if (input[j] == '-')
  248.                 {
  249.                     Sudoku[i, j] = 0;
  250.                     canBeChanged[i,j] = true;
  251.                     continue;
  252.                 }
  253.                 else
  254.                 {
  255.                     Sudoku[i, j] = int.Parse(input[j].ToString());
  256.                     canBeChanged[i, j] = false;
  257.                 }
  258.             }
  259.         }
  260.  
  261.         try
  262.         {
  263.             SudokuSolver(0, 0);
  264.         }
  265.         catch (Exception)
  266.         {
  267.             PrintSudoku();
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement