5,660,782 members and growing! (15,459 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » Games License: The Code Project Open License (CPOL)

Simple Linq Sudoku Solver

By Mickael Magniez

A simple way to resolve a sudoku grid, in 10 lines of code.
C# (C# 3.0, C#), LINQ

Posted: 5 Sep 2008
Updated: 5 Sep 2008
Views: 5,920
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 6.22 Rating: 4.39 out of 5
1 vote, 3.8%
1
2 votes, 7.7%
2
1 vote, 3.8%
3
3 votes, 11.5%
4
19 votes, 73.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

A simple solution to resolve a Sudoku Grid, using Linq.

You can download a winform example using this method

sudoku.png

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

Using the code 

The solver take a list of integer, and return the solution in 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 take an empty cell, try 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 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 test 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, foreach possible value, fills the empty cell and recall the solver.
grid[emptyCell.index] = trying;
if ((_cells = solver(grid)).Count > 0 )
  return _cells; 

History


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



Occupation: Software Developer (Junior)
Location: France France

Other popular Game Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralBrute force solutionmemberbearskin15:09 8 Sep '08  
GeneralSimplificationmemberbearskin14:49 8 Sep '08  
GeneralAwesomemembermerlin9817:07 5 Sep '08  
GeneralCool!memberRob Philpott2:49 5 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Sep 2008
Editor:
Copyright 2008 by Mickael Magniez
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project