Код:
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
class SudokuSolver
{
private int[] grid;
public SudokuSolver(String s)
{
grid = new int[81];
for (int i = 0; i < s.Length; i++)
{
grid[i] = int.Parse(s[i].ToString());
}
}
public void solve()
{
try
{
placeNumber(0);
Console.WriteLine("Судоку которое было нарисовано Васей не правильно");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(this);
}
}
public void placeNumber(int pos)
{
if (pos == 81)
{
Console.ForegroundColor = ConsoleColor.Red;
throw new Exception("\nСудоку которое было нарисовано Васей правильно.\n");
}
if (grid[pos] > 0)
{
placeNumber(pos + 1);
return;
}
else if (grid[pos] == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
throw new Exception("\nСудоку которое было нарисовано Васей не правильно.\n");
}
for (int n = 1; n <= 9; n++)
{
if (checkValidity(n, pos % 9, pos / 9))
{
grid[pos] = n;
placeNumber(pos + 1);
grid[pos] = 0;
}
}
}
public bool checkValidity(int val, int x, int y)
{
for (int i = 0; i < 9; i++)
{
if (grid[y * 9 + i] == val || grid[i * 9 + x] == val)
return false;
}
int startX = (x / 3) * 3;
int startY = (y / 3) * 3;
for (int i = startY; i < startY + 3; i++)
{
for (int j = startX; j < startX + 3; j++)
{
if (grid[i * 9 + j] == val)
return false;
}
}
return true;
}
public override string ToString()
{
Console.ForegroundColor = ConsoleColor.Yellow;
string sb = "";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
sb += (grid[i * 9 + j] + " ");
if (j == 2 || j == 5)
sb += ("| ");
}
sb += ('\n');
if (i == 2 || i == 5)
sb += ("------+-------+------\n");
}
return sb;
}
static void Main(String[] args)
{
Console.Title = "Программа для проверки решения судоку.";
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.SetWindowSize(Console.LargestWindowWidth - 3, Console.LargestWindowHeight - 2);
Console.Write("************************************************************************************************");
Console.Write("\nУсловие программы: " +
"Судоку размера n называется квадрат со стороной n2, разделенный на n2" +
"\nсредних квадратов со стороной n, каждый из которых разделен на n2 маленьких квадратов." +
"\nВ каждом маленьком квадрате записано число от 1 до n2." +
"\nСудоку называется правильным, если в каждом столбце," +
"\nкаждой строке и каждом среднем квадрате встречаются все числа от 1 до n2." +
"\nНедавно Вася нарисовал Судоку размера n." +
"\nВаша задача – помочь ему определить правильный ли он.");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\n************************************************************************************************");
Console.ForegroundColor = ConsoleColor.Red;
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nДля продолжения нажмите любую клавишу . . . ");
Console.ReadKey();
}
Console.Clear();
string stroka = null;
string[] reader = File.ReadAllLines("Input.txt");
StreamReader file = new StreamReader("Input.txt");
foreach (string buf in reader)
{
string bufStroka = file.ReadLine();
stroka = stroka + bufStroka;
}
stroka = stroka.Replace(" ", string.Empty);
new SudokuSolver(stroka).solve();
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nПрограмма завершена, нажмите любую клавишу . . . ");
Console.ReadKey();
}
}
}
То что лежит в файле
3 7 9 2 4 6 8 1 5
5 2 8 1 3 7 6 9 4
1 6 4 5 8 9 2 7 3
8 4 7 3 5 2 1 6 9
6 1 3 8 9 4 5 2 7
2 9 5 6 7 1 3 4 8
4 3 1 9 6 8 7 5 2
7 5 2 4 1 3 9 8 6
9 8 6 7 2 5 4 3 1