What I am developing is that initially the entire sudoku board is empty. One of the random cells(out of 81) is filled with a random value(1-9).
Now I want to fill all the remaining cells using brute force approach.
From what I came to know after googling is that we should start with the first cell and fill it with 1(if it's valid), then fill the second cell with 2(if it's valid, we will begin checking with a number greater than the last filled cell, which in this case is 1, once we reach 9, we reset it with 1).
The thing is that it's not working properly!
Can anyone link me to the exact algorithm.
Here's an implementation of the backtracking approach:
This simple random walk algorithm should work too (but is inefficient- use at your own risk!!!):
EDIT: - added fix for unresolvable solutions.
EDIT 2
If someone are interested here are described methods for solving sudoku with random-based search.
There are a few algorithms outlined on Algorithmics of sudoku. What you're describing sounds like a backtracking approach.
I used a method without backtracing, although the while loop might be it. To quote an algorithm book I've read "Nothing in recursion can't be duplicated using iteration".
I've been using my eyes to inspect this, and since I can't wrap my head around the recursive method, even though recursion is relatively understood:
This method, I kinda wrote with some guidance, had a bug in the grid checker, when I found it, it seems to be working now. I'm positing it 'cause it's hard to find complete-and-working code. IOS SDK.
Note: The header file is empty, paste this into iOS single View application if you desire.
Caution: might loop infinitely( and above does sometimes, but is very fast), may want another more global "tries" variable, and restart the algorithm as a safety, or give it a seed/do both
edit: the below should be safe from infinite loops, if the source grid is solvable (or nonexistant)
Summary: The first version is flawed but (mostly) gets the job done. It generates every row at random, if the row is invalid, it wipes and starts over. This will wipe out source grids, and can go forever, but works most of the time.
The lower code uses recursion. I don't think it backtracks properly, but it has solved empty and semi-seeded grids on my tests. I think I need to save a "state" grid to backtrack with, but I'm not doing this. I'm posting both since they both answer "Brute force"... on my own, I should study recursion, I can't explain why the lower one works, I personally could use help with doing it.
Note: The first one finishes in a blink or so when it does finish... if speed means more than reliability to your application (somewhat counter-intuitive in this case, with the infinite looping, heh).
I recently did a series in my blog on creating a Sudoku solver in C#; you can probably adapt the simple backtracking algorithm I present to your purposes.
http://blogs.msdn.com/b/ericlippert/archive/tags/graph+colouring/
Have a look at the following. Note that I have not run it, so I can't vouch for its claims:
http://www.codeproject.com/KB/game/SudokuGen.aspx
The code is in VB.NET, but the algorithm will be the same in C#.
There is a C# version here:
http://www.codeproject.com/KB/game/sudokuincsharp.aspx
The link supplied by @Bill the Lizard does a nice job explaining things, as opposed to the implementation links I supplied above.