Click here to Skip to main content
Licence CPOL
First Posted 4 Sep 2008
Views 20,886
Downloads 245
Bookmarked 34 times

Simple LINQ Sudoku Solver

By | 4 Sep 2008 | Article
A simple way to resolve a Sudoku grid, in 10 lines of code.

Introduction

This is a simple solution to resolve a Sudoku Grid, using LINQ. You can download a WinForm example that uses this method from the above link.

sudoku.png

It solves all grids I tested in less than 1 second.

Using the code

The solver takes a list of integers, and returns the solution in the same type.

private List<int> solver(List<int> _cells)
{
  var emptyCell = _cells.Select((val, index) => 
      new { index, val }).FirstOrDefault(cell => cell.val == 0);
  if (emptyCell == null)
    return _cells;
  List<int> grid = new List<int>(_cells);

  foreach (int trying in Enumerable.Range(1, 9).Except(_cells.Where((val, index) => 
               sameRowColBox(index,emptyCell.index)).Distinct()))
  {
    grid[emptyCell.index] = trying;
    if ((_cells = solver(grid)) != null)
      return _cells;
  }
  return null;
}

Briefly, the function takes an empty cell, tries to fill it with a correct value, and recursively calls the solver with this new grid.

The recursion stops when a correctly filled grid is found, or when all possibilities have been explored:

if (emptyCell == null)
    return _cells;

Choose the first empty cell in the current grid, and get its index, thanks to the Enumerable.Select method:

var emptyCell = _cells.Select((val, index) => 
        new { index, val }).FirstOrDefault(cell => cell.val == 0); 

Take all the possible values for this cell:

Enumerable.Range(1, 9).Except(_cells.Where((val, index) => 
       sameRowColBox(index,emptyCell.index)).Distinct())

This function tests if two indexes are 'in conflict": same row; column, or 3*3 box.

private bool sameRowColBox(int i, int j){
  return (i / 9 == j / 9) || (i % 9 == j % 9) || (((i % 9) / 
            3 + (i / 9) / 3 * 3) == ((j % 9) / 3 + (j / 9) / 3 * 3));
}

And then, for each possible value, fills the empty cell and recalls the solver.

grid[emptyCell.index] = trying;
if ((_cells = solver(grid)).Count > 0 )
  return _cells;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mickael Magniez

Software Developer (Junior)

France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberAnurag Gandhi20:05 13 Dec '11  
GeneralMy vote of 5 Pinmvpthatraja5:00 18 Jan '11  
GeneralBrute force solution Pinmemberbearskin14:09 8 Sep '08  
GeneralSimplification Pinmemberbearskin13:49 8 Sep '08  
I'd change this code:
 
private bool sameRowColBox(int i, int j){
  return (i / 9 == j / 9) || (i % 9 == j % 9) || (((i % 9) / 3 + (i / 9) / 3 * 3) == ((j % 9) / 3 + (j / 9) / 3 * 3));
}
To this:
private bool sameRowColBox(int i, int j){
	int irow = i / 9, icol = i % 9;
	int jrow = j / 9, jcol = j % 9;
	return (irow == jrow) ||
		(icol == jcol) ||
		(boxNum(irow, icol) == boxNum(jrow, jcol));
}
And implement boxNum() as either:
private int boxNum (int row, int col)
{
	return col / 3 + row / 3 * 3;
}
or:
private int boxNum (int row, int col)
{
	int [] colBox = {0,0,0, 1,1,1, 2,2,2};
	int [] rowBox = {0,0,0, 3,3,3, 6,6,6};
	return rowBox[row] + colBox[col];
}
The intent is a bit clearer.
GeneralAwesome Pinmembermerlin9816:07 5 Sep '08  
GeneralCool! PinmemberRob Philpott1:49 5 Sep '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120604.1 | Last Updated 5 Sep 2008
Article Copyright 2008 by Mickael Magniez
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid