Connecting Tech Pros Worldwide Help | Site Map

Sudoku C

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#1   May 12 '07
Greetings,

welcome back to part three of the Sudoku article. This part describes the actual
Sudoku solver. The solver works quite simple: given a cell to be filled in,
try all possible values and solve the rest of the board. If all cells are filled
the Sudoku puzzle is solved.

The solver iterates over all cells, left to rigth, top to bottom. It receives
two parameters i and j which are the indexes of the cell to be filled in. The
cell could be filled in already (it was a given cell value) in which case the
solver has to find a next position. The first part of the solve method tries
to find an empty cell:
Expand|Select|Wrap|Line Numbers
  1. private boolean solve(int i, int j) {
  2.  
  3.     for(;;) {
  4.         if (j >= columns.length) {
  5.             j= 0;
  6.             i++;
  7.         }
  8.  
  9.         if (i >= rows.length) return true;
  10.  
  11.         if (board[i][j] > 0) j++;
  12.         else break;
  13.     }
  14.     ...
If you carefully inspect this piece of code you see that every row is checked,
column by column. When the last column has been checked a next row is checked
again until all cells are checked in which case the Sudoku puzzle is solved
and true is returned. Otherwise an empty cell has been found and the piece of
the code following this for loop is executed. The next piece of this method
recursively tries to solve the Sudoku puzzle:
Expand|Select|Wrap|Line Numbers
  1.     ...
  2.     for (int val= 0; val < squares.length; val++) {
  3.         if (possible(i, j, val)) 
  4.             if (!solve(i, j+1)) {
  5.                 reset(i, j);
  6.             }
  7.             else
  8.                 return true;
  9.     }
  10.  
  11.     return false;
  12. }
For the current cell all values 0 ... 8 are tried. If a value is feasible
it is filled in and an attempt is made to solve the rest of the Sudoku puzzle.
If the attempt fails the cell is reset and a next value is tried. If the
attempt was successful the method returns immediately because the entire
puzzle was solved. Otherwise the loop ends (none of the values were feasible
for that particular cell i,j) and the method returns false.

Note that this method is a private method. I did that on purpose because I
don't want other objects to supply the two index values. Here's a public
method that does it for them:
Expand|Select|Wrap|Line Numbers
  1. public boolean solve() {
  2.     return solve(0, 0);
  3. }
All we need now is a simple driver method that creates a Sudoku solver object,
opens a Reader, initializes the puzzle and fires up the solver. We'll implement
the driver functionality in the main() method for reasons of simplicity:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) throws Exception {
  2.  
  3.     Sudoku s= new Sudoku();
  4.     FileReader fr= new FileReader(args[0]);
  5.  
  6.     if (s.read(new FileReader(args[0))) {
  7.         if (s.solve()) {
  8.             s.print(new OutputStreamWriter(System.out));
  9.         }
  10.         else
  11.             System.err.println("problem cannot be solved");
  12.     }
  13.     else
  14.         System.err.println("problem file cannot be read");
  15. }
Note that this is a very sloppy implementation, i.e. when something goes
seriously wrong, e.g. no file name was passed to the main method or the
actual reading failed, an Exception is simply passed on to the JVM which will
print an ugly stack trace. We even didn't bother to close the FileStream
properly: when the JVM exits the FileReader will be closed anyway.

I leave it up to your imagination and creativity to implement a proper,
industrial strength driver. The one above serves fine for demonstration
purposes in this article.

I created a file "/sudoku.txt" with the following content I found in an
old newspaper:
Expand|Select|Wrap|Line Numbers
  1. +-------+-------+-------+
  2. | 7 . . | . . . | 4 . . | 
  3. | . 2 . | . 7 . | . 8 . |
  4. | . . 3 | . . 8 | . . 9 |
  5. +-------+-------+-------+ 
  6. | . . . | 5 . . | 3 . . |
  7. | . 6 . | . 2 . | . 9 . |
  8. | . . 1 | . . 7 | . . 6 |
  9. +-------+-------+-------+ 
  10. | . . . | 3 . . | 9 . . |
  11. | . 3 . | . 4 . | . 6 . |
  12. | . . 9 | . . 1 | . . 5 |
  13. +-------+-------+-------+
Then I started the Sudoku class:
Expand|Select|Wrap|Line Numbers
  1. java -classpath . Sudoku /sudoku.txt
And this is what was printed almost immediately:
Expand|Select|Wrap|Line Numbers
  1. +-------+-------+-------+
  2. | 7 9 8 | 6 3 5 | 4 2 1 | 
  3. | 1 2 6 | 9 7 4 | 5 8 3 | 
  4. | 4 5 3 | 2 1 8 | 6 7 9 | 
  5. +-------+-------+-------+
  6. | 9 7 2 | 5 8 6 | 3 1 4 | 
  7. | 5 6 4 | 1 2 3 | 8 9 7 | 
  8. | 3 8 1 | 4 9 7 | 2 5 6 | 
  9. +-------+-------+-------+
  10. | 6 1 7 | 3 5 2 | 9 4 8 | 
  11. | 8 3 5 | 7 4 9 | 1 6 2 | 
  12. | 2 4 9 | 8 6 1 | 7 3 5 | 
  13. +-------+-------+-------+
I checked the next days' newpaper and indeed: both the newspaper as well as the
solver showed this same solution. Just for fun I changed the content of my
/sudoku.txt file:
Expand|Select|Wrap|Line Numbers
  1. +-------+-------+-------+
  2. | . . . | . . . | . . . | 
  3. | . . . | . . . | . . . |
  4. | . . . | . . . | . . . |
  5. +-------+-------+-------+ 
  6. | . . . | . . . | . . . |
  7. | . . . | . . . | . . . |
  8. | . . . | . . . | . . . |
  9. +-------+-------+-------+ 
  10. | . . . | . . . | . . . |
  11. | . . . | . . . | . . . |
  12. | . . . | . . . | . . . |
  13. +-------+-------+-------+
and fired up the solver again; this was the output:
Expand|Select|Wrap|Line Numbers
  1. +-------+-------+-------+
  2. | 1 2 3 | 4 5 6 | 7 8 9 | 
  3. | 4 5 6 | 7 8 9 | 1 2 3 | 
  4. | 7 8 9 | 1 2 3 | 4 5 6 | 
  5. +-------+-------+-------+
  6. | 2 1 4 | 3 6 5 | 8 9 7 | 
  7. | 3 6 5 | 8 9 7 | 2 1 4 | 
  8. | 8 9 7 | 2 1 4 | 3 6 5 | 
  9. +-------+-------+-------+
  10. | 5 3 1 | 6 4 2 | 9 7 8 | 
  11. | 6 4 2 | 9 7 8 | 5 3 1 | 
  12. | 9 7 8 | 5 3 1 | 6 4 2 | 
  13. +-------+-------+-------+
The output clearly shows that the solver works its way through the problem in
a strictly left to right, top to bottom manner: check the first row and the
first sub-square and see the regularity.

This is a naive solver but it works fine for the problems I tried. Some problems
are considered to be extremely difficult. Play with this solver a bit, feed it
some of those difficult problems and see how it runs.

The Sudoku class only needs a Reader and a Writer, i.e. it doesn't care how
these streams were created. You can even create a free Sudoku solver server
out of it if you feel like it: wrap a Reader around an InputStream created
by a socket. Do the same for the Writer and the socket's OutputStream.

This solver finds the 'first' solution given a problem. You might try and
alter the 'solve' method a bit so that it finds all solutions. It's up to you.

Until next week when a bit more serious matters will be discussed a bit.

kind regards,

Jos



Newbie
 
Join Date: Dec 2007
Posts: 1
#2   Dec 11 '07

re: Sudoku C


We classify difficulty levels as Easy, Medium and Hard. I had used Observer Pattern to solve easy ones . I hope your solution would solve all levels.
Reply