Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SudokuSolver
  7. {
  8.     public class Program
  9.     {
  10.         private static readonly string[] allowedNumbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  11.         private static readonly string emptyBox = "x";
  12.  
  13.         private static string[,] sudoku = new string[9, 9];
  14.        
  15.         static void Main()
  16.         {
  17.             for (int i = 0; i < sudoku.GetLength(0); i++)
  18.             {
  19.                 string input = Console.ReadLine();
  20.                 string[] sudokuRow = input.Trim().Split().ToArray();
  21.  
  22.                 if (sudokuRow.Length != 9)
  23.                 {
  24.                     throw new ArgumentException("Provided data contains more or less than 9 symbols");
  25.                 }
  26.  
  27.                 foreach (var symbol in sudokuRow)
  28.                 {
  29.                     isValidString(symbol);
  30.                 }
  31.  
  32.                 for (int j = 0; j < sudoku.GetLength(1); j++)
  33.                 {
  34.                     sudoku[i, j] = sudokuRow[j];
  35.                 }
  36.             }
  37.  
  38.             // test printing
  39.             var printSudoku = PrintSudoku(sudoku);
  40.             Console.Write(printSudoku);
  41.  
  42.             SolveSudoku(sudoku, 0, 0);
  43.         }
  44.  
  45.         static void isValidString(string str)
  46.         {
  47.             if (!allowedNumbers.Contains(str) && str.ToLower() != emptyBox.ToLower())
  48.             {
  49.                 throw new ArgumentException("Provided data contains one or more invalid symbols!");
  50.             }
  51.         }
  52.  
  53.         static string PrintSudoku(string[,] sudoku)
  54.         {
  55.             var horizontalSeparator = "+-------+-------+-------+ ";
  56.             var verticalSeparator = "| ";
  57.             var stringBuilder = new StringBuilder();
  58.  
  59.             for (int i = 0; i < sudoku.GetLength(0); i++)
  60.             {
  61.                 if (i == 0 || i % 3 == 0)
  62.                 {
  63.                     stringBuilder.AppendFormat(horizontalSeparator + Environment.NewLine);
  64.                 }
  65.  
  66.                 for (int j = 0; j < sudoku.GetLength(1); j++)
  67.                 {
  68.                     if (j == 0 || j % 3 == 0)
  69.                     {
  70.                         stringBuilder.Append(verticalSeparator);
  71.                     }
  72.  
  73.                     if (sudoku[i,j].ToLower() == emptyBox.ToLower())
  74.                     {
  75.                         stringBuilder.Append("  ");
  76.                     }
  77.                     else
  78.                     {
  79.                         stringBuilder.Append(sudoku[i, j] + " ");
  80.                     }
  81.                    
  82.                 }
  83.  
  84.                 stringBuilder.Append(verticalSeparator);
  85.                 stringBuilder.Append(Environment.NewLine);
  86.             }
  87.  
  88.             stringBuilder.AppendFormat(horizontalSeparator + Environment.NewLine);
  89.  
  90.             var result = stringBuilder.ToString();
  91.             return result;
  92.         }
  93.  
  94.         static List<string> CheckRow(string[,] sudoku, int row)
  95.         {
  96.             List<string> rowAsList = new List<string>();
  97.             for (int j = 0; j < sudoku.GetLength(1); j++)
  98.             {
  99.                 if (sudoku[row,j] != emptyBox)
  100.                 {
  101.                     rowAsList.Add(sudoku[row, j]);
  102.                 }
  103.             }
  104.  
  105.             List<string> result = allowedNumbers.Except(rowAsList).ToList();
  106.            
  107.             return result;
  108.         }
  109.  
  110.         static List<string> CheckCol(string[,] sudoku, int col)
  111.         {
  112.             List<string> colAsList = new List<string>();
  113.             for (int i = 0; i < sudoku.GetLength(0); i++)
  114.             {
  115.                 if (sudoku[i, col] != emptyBox)
  116.                 {
  117.                     colAsList.Add(sudoku[i, col]);
  118.                 }
  119.             }
  120.  
  121.             List<string> result = allowedNumbers.Except(colAsList).ToList();
  122.  
  123.             return result;
  124.         }
  125.  
  126.         static List<string> CheckSquare(string[,] sudoku, int row, int col)
  127.         {
  128.             int startRow = ((row / 3) * 3);
  129.             int startCol = ((col / 3) * 3);
  130.  
  131.             List<string> squareAsList = new List<string>();
  132.             for (int i = startRow; i < startRow + 3; i++)
  133.             {
  134.                 for (int j = startCol; j < startCol + 3; j++)
  135.                 {
  136.                     if (sudoku[i,j] != emptyBox)
  137.                     {
  138.                         squareAsList.Add(sudoku[i, j]);
  139.                     }
  140.                 }
  141.             }
  142.  
  143.             List<string> result = allowedNumbers.Except(squareAsList).ToList();
  144.  
  145.             return result;
  146.         }
  147.  
  148.         static List<string> CheckIntersection(List<string> checkRow, List<string> checkCol, List<string> checkSquare)
  149.         {
  150.             List<string> intersection = checkRow.Intersect(checkCol).Intersect(checkSquare).ToList();
  151.             return intersection;
  152.         }
  153.  
  154.         static void SolveSudoku(string[,] sudoku, int row = 0, int col = 0)
  155.         {
  156.             if (sudoku[row, col].ToLower() == emptyBox.ToLower())
  157.             {
  158.                 var rowCheck = CheckRow(sudoku, row);
  159.                 var colCheck = CheckCol(sudoku, col);
  160.                 var squareCheck = CheckSquare(sudoku, row, col);
  161.                 var proposals = CheckIntersection(rowCheck, colCheck, squareCheck);
  162.  
  163.                 if (proposals.Count == 0)
  164.                 {
  165.                     return;
  166.                 }
  167.  
  168.                 foreach (var proposal in proposals)
  169.                 {
  170.                     sudoku[row, col] = proposal;
  171.                     SolveSudoku(sudoku, row, col);
  172.                     sudoku[row, col] = emptyBox;
  173.                 }
  174.             }
  175.             else
  176.             {
  177.                 if (col + 1 < sudoku.GetLength(1))
  178.                 {
  179.                     col += 1;
  180.  
  181.                     SolveSudoku(sudoku, row, col);
  182.                 }
  183.                 else if (row + 1 < sudoku.GetLength(0))
  184.                 {
  185.                     col = 0;
  186.                     row += 1;
  187.  
  188.                     SolveSudoku(sudoku, row, col);
  189.                 }
  190.                 else if (col == (sudoku.GetLength(1) - 1) && (row == sudoku.GetLength(0) - 1))
  191.                 {
  192.                     Console.WriteLine(PrintSudoku(sudoku));
  193.  
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement