Two Laptop Bag*
The moose likes Java in General and the fly likes Sudoku solver help (not brute force) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sudoku solver help (not brute force)" Watch "Sudoku solver help (not brute force)" New topic

Sudoku solver help (not brute force)

David Phluphy

Joined: Sep 09, 2010
Posts: 25

I'm working on a sudoku solver that solves sudoku like a novice human sudoku solver would.

I start off by making a candidate list for each of the 81 boxes, a 2d array of arralylists (ArrayList[][] candidates).
Then I strip away candidates from the lists, and insert the element if there's just one candidate. I think the code is correct for this(?)
Then I check for hidden singles, which is a lot harder. I think the code for rows and columns should be okay, but I don't really know how to handle it for boxes.

Any help would be greatly appreciated, also, please let me know if this method is completely retarded.


Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7488

David Phluphy wrote:Any help would be greatly appreciated, also, please let me know if this method is completely retarded.

Well you've plainly put some thought into it, so it's unlikely to be retarded, even if it doesn't completely work.

This page might help.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I once wrote a sudoku solver using a boolean[][][]. Each single boolean is an indicator whether a value can be in that position. For instance the checking if 9 can stand on the first row, second column would be if(options[0][1][8]). This allows you to search for patterns. In your design I don't really see how you can find these patterns (especially the more complicated onces). Maybe you should rethink your design.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Phluphy

Joined: Sep 09, 2010
Posts: 25
It turns out the puzzle I was trying to solve was actually too hard, and couldn't be solved with just these techniques. I switched to a different puzzle, and it solved it right away. I'm still interested too see if anyone has any clever ideas for finding hidden singles in a sub-box though, so I'm going to leave the thread open for a while

Thanks to Wouter and Winston for contributing.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 3573

This may be out the scope of the discussion, but I once solved the hidden single in a much more general way. Hidden singles aren't necessarily "singles". With blocks, they are actually "triplets".

My theory is that if all the candidates of a group (row, column or block) for a particular digit are also candidates for one other group (I use the word "intersect"), you can eliminate that digit as a candidate for all other cells in the two intersecting groups. For an intersecting block and row/column, this usually leaves a triplet of candidates.

In the example, you can eliminate 2, 4 and 6 as candidates from the cells marked by *. Each of these digits is a candidate of a cell where the column intersects with one other group, so you can eliminate them in the remainder of the two groups, which is trivial for the column, but gives you new information about the block.

You can extend this theory to all sorts of soduku types. It works really well on X-sudokus and the sudokus which have the 4 grey blocks. You just need to determine in which cells two groups intersect, if at all.

Granted, I tested my strategy using a functional language, which may be much easier than in Java. Heed Wouter's suggestion of using a boolean array to check for patterns.

[Thumbnail for triplet.png]

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 3573

Actually, sorry. I got confused by the terminology here. The suggestion I made has nothing to do with hidden singles. It's just a general strategy for elimination. It may be useful regardless.
David Phluphy

Joined: Sep 09, 2010
Posts: 25
Thanks for the input guys

I'm not familiar with patterns, and I don't want to sound stubborn, but I -really- want to solve it the way I started. This solver is not supposed to be able to solve every single sudoku-puzzle, just the ones with naked singles and hidden singles. I've updated the code, but my mind is a bit tired. If anyone knows a puzzle with just naked singles and hidden singles, please show it to me so I can test it.

Ashish Schottky
Ranch Hand

Joined: Dec 29, 2009
Posts: 93
Here is a puzzle that is solved by hidden singles alone.
{ {1,0,0, 2,0,0, 3,0,0},
{0,2,0, 0,1,0, 0,4,0},
{0,0,3, 0,0,5, 0,0,6},
{7,0,0, 6,0,0, 5,0,0},
{0,5,0, 0,8,0, 0,7,0},
{0,0,8, 0,0,4, 0,0,1},
{8,0,0, 7,0,0, 4,0,0},
{0,3,0, 0,6,0, 0,2,0},
{0,0,9, 0,0,2, 0,0,7}};

I used the following strategy, a candidate 'x' that has only one cell to go and other candidates have legal possibility to occupy that cell, then candidate is said to be the hidden single.

I used this strategy to solve.
1) First pencil up the candidates.
2) Check for naked singles.
3)Update the candidates.
4)Check for hidden singles.

To check for hidden singles, just try if a candidate can occupy more than one cell that row and column, then it is not hidden, else its hidden candidate.
David Phluphy

Joined: Sep 09, 2010
Posts: 25
Thanks Ashish, my method is definitely not working D:
Guess I'll take a closer look at it tomorrow.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340

david, you sound so determined that i am sure you will come up with a great solution. i have also thought a sudoku solver would make a good project but i never tried it. another great idea for a project would be a sudoku generator.

Visit my download page
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37890
I have edited some code by breaking the long lines; they make it difficult to read. I hope nobody has quoted line numbers anywhere because I’ve changed them all.
David Phluphy

Joined: Sep 09, 2010
Posts: 25

I've tidied up the code a bit so It'll be easier to understand. It still can't solve for hidden single though, so if anyone has any ideas ... ^^
Any other comments are welcome too.

Edit: I've renamed some variables to make it easier to read.

David Phluphy

Joined: Sep 09, 2010
Posts: 25
Finally got it

It still outputs fail, but that's only because I haven't added the solution ^^'

Consider Paul's rocket mass heater.
subject: Sudoku solver help (not brute force)
Similar Threads
Programing a sudoku solver
Exception -- java.lang.StackOverflowError
simple sorting algorithm
SoDuko puzzle
Sudoku Solver