Sudoku
A sudoku puzzle is a grid of nine by nine cells, that has been subdivided into nine subgrids of three by three cells.
The objective of sudoku is to enter a digit from 1 through 9 in each cell, in such a way that:
1. Each horizontal row contains each digit exactly once
2. Each vertical column contains each digit exactly once
3. Each subgrid contains each digit exactly once
public static void PrintSudoku(int[,] puzzle)
{
Console.WriteLine("+-----+-----+-----+");
for (int i = 1; i < 10; ++i)
{
for (int j = 1; j < 10; ++j)
Console.Write("|{0}", puzzle[i - 1, j - 1]);
Console.WriteLine("|");
if (i % 3 == 0) Console.WriteLine("+-----+-----+-----+");
}
}
public static bool SolveSudoku(int[,] puzzle, int row, int col)
{
if (row < 9 && col < 9)
{
if (puzzle[row, col] != 0)
{
if ((col + 1) < 9) return SolveSudoku(puzzle, row, col + 1);
else if ((row + 1) < 9) return SolveSudoku(puzzle, row + 1, 0);
else return true;
}
else
{
for (int i = 0; i < 9; ++i)
{
if (IsAvailable(puzzle, row, col, i + 1))
{
puzzle[row, col] = i + 1;
if ((col + 1) < 9)
{
if (SolveSudoku(puzzle, row, col + 1)) return true;
else puzzle[row, col] = 0;
}
else if ((row + 1) < 9)
{
if (SolveSudoku(puzzle, row + 1, 0)) return true;
else puzzle[row, col] = 0;
}
else return true;
}
}
}
return false;
}
else return true;
}
private static bool IsAvailable(int[,] puzzle, int row, int col, int num)
{
int rowStart = (row / 3) * 3;
int colStart = (col / 3) * 3;
for (int i = 0; i < 9; ++i)
{
if (puzzle[row, i] == num) return false;
if (puzzle[i, col] == num) return false;
if (puzzle[rowStart + (i % 3), colStart + (i / 3)] == num) return false;
}
return true;
}
Example
int[,] puzzle = {
{ 3, 2, 1, 7, 0, 4, 0, 0, 0 },
{ 6, 4, 0, 0, 9, 0, 0, 0, 7 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 4, 5, 9, 0, 0 },
{ 0, 0, 5, 1, 8, 7, 4, 0, 0 },
{ 0, 0, 4, 9, 6, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 2, 0, 0, 0, 7, 0, 0, 1, 9 },
{ 0, 0, 0, 6, 0, 9, 5, 8, 2 }
};
if (SolveSudoku(puzzle, 0, 0))
PrintSudoku(puzzle);
Output
+-----+-----+-----+
|3|2|1|7|5|4|6|9|8|
|6|4|8|2|9|3|1|5|7|
|5|7|9|8|1|6|2|3|4|
+-----+-----+-----+
|7|8|2|3|4|5|9|6|1|
|9|6|5|1|8|7|4|2|3|
|1|3|4|9|6|2|8|7|5|
+-----+-----+-----+
|8|9|3|5|2|1|7|4|6|
|2|5|6|4|7|8|3|1|9|
|4|1|7|6|3|9|5|8|2|
+-----+-----+-----+