I've written a few of these in my time in various languages from VBA in Excel through to Smalltalk. Mostly I adopt a method of trying a few different simple heuristics and resorting to a recursive backtracking method if they fail. The typical outline is something like this:
Solve (game)
if game is complete return SUCCESS
if heuristic 1 works
return Solve (game)
if heuristic 2 works
return Solve (game)
return Solve (game)
...
if heuristic n works
return Solve (game)
choose next empty cell
for each valid number for this cell
if Solve (game) then return SUCCESS
next
return FAILURE
This works well and using heuristics such as identifying cells that can only take one value or identifying where a value only appears once in a row or column or square are enough to solve the board very rapidly. The nice thing about the above algorithm is that you can easily maintain complex state information about the board and just copy that over to the next call letting the stack do all the work for you instead of having to restore the state.
Dumbed down solver
For this version I decided to take a very simple approach with no heuristics and just trying all valid numbers in each successive square. I also converted the more elegant and simple recursive approach to an iterative approach and make the whole thing as "stateless" as possible. Here is the result...
using System; using System.Collections.Generic; using System.Linq; namespace Sudoku { class Program { static int[,] groups = new int[81, 20]; static void Setup() { //for each square, create a list of all the related squares and store in groups[] int [] start = new [] {0, 3, 6, 27, 30, 33, 54, 57, 60}; //top left of each square int [] index = new [] {0, 1, 2, 9, 10, 11, 18, 19, 20}; //offset for 9 elements of square for (int r = 0; r < 9; r++) { for (int c = 0; c < 9; c++) { int k = start[c / 3 + 3 * (r / 3)]; //start of square that this r,c belongs to List<int> tmp = new List<int>(); for (int i = 0; i < 9; i++) { tmp.Add(i + r * 9); //row tmp.Add(c + i * 9); //column tmp.Add(k + index[i]); //square } tmp = tmp.Distinct().ToList(); int s = c + r * 9; tmp.Remove(s); for (int i = 0; i < tmp.Count(); i++) groups[s, i] = tmp[i]; } } } static int[] Solve(int[] grid) { int[] b = new int[81]; List<int>[] valid = new List<int>[81]; int [] empty = new int[81]; int numEmpty = 0; for (int i = 0; i < 81; i++) { b[i] = grid[i]; if (b[i] == 0) empty[numEmpty++] = i; } int nodes = 0; int s = 0; //create list of valid numbers for square s by eliminating all numbers in related squares valid[empty[s]] = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); for (int i = 0; i < 20; i++) valid[empty[s]].Remove(b[groups[empty[s], i]]); while (s < numEmpty && s >= 0) { if (valid[empty[s]].Count > 0) { nodes++; b[empty[s]] = valid[empty[s]].ElementAt(0); valid[empty[s]].RemoveAt(0); s++; if (s < numEmpty) { valid[empty[s]] = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); for (int i = 0; i < 20; i++) valid[empty[s]].Remove(b[groups[empty[s], i]]); } } else { b[empty[s]] = 0; s--; } } return b; } static void Main(string[] args) { Setup(); int[] b = Solve(new int[] {3, 0, 0, 9, 0, 7, 1, 8, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 5, 1, 0, 0, 0, 3, 0, 6, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 5, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 9, 0, 3, 0, 0, 0, 1, 7, 0, 0, 0, 0, 0, 4, 9, 0, 0, 0, 0, 0, 2, 4, 7, 0, 8, 0, 0, 0}); for (int r = 0; r < 9; r++) { string s = ""; for (int c = 0; c < 9; c++) { int i = c + r * 9; s = s + b[i].ToString() + " "; } Console.WriteLine(s); } Console.ReadKey(); } } }
The program has a small setup section that need only be run once. It sets up an array containing all the related cells for every cell in turn. That means all the cells that are in either the same row, column or 9-square. Note that there are only 20 related cells: 8 within the 9-square and 6 for each of row and column.
I ran some tests and on my (not very up-to-date) machine it solved 100 puzzles in about 3.5 seconds, so 35 hundredths of a second per solution.
Dancing Links, Graph Theory and the 4 Colour problem
Apparently the standard way of solving a problem like this is to using a method based on doubly-linked lists called "Dancing Links" (coined by Knuth). I haven't looked into how that works and how much improvement it would offer although I believe I read that it halved the time for the solution to a related problem.
Sudoku belongs to the same set of problems as the famous 4 colour problem. The 4 colour theorem (no longer a conjecture since it was proved... although there are some sections of the maths community that are less than satisfied since the proof relied heavily of computational aids) states that it is always possible to colour a map so that every region is a different colour to it's neighbouring regions using only 4 colours. This problem can be reformulated as a graph where each of the regions represents a node and the connections represent the boundaries. Now you need to ensure that all connected nodes have a different label (colour).
The Sudoku problem can be reformulated in a similar way. Each cell represents a node is connected to the 20 related cells. That means 81 nodes and 810 connections (1/2 * 81 * 20), i.e. every node has 20 connections, but a->b is the same as b->a so we need to half the count.
I've not looked at whether there are standard graph colouring algorithms that can help, but I suspect not since they would necessarily cater for more general problems, i.e. without the high degree of structure in Sudoku.
