• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Sudoku solver help (not brute force)

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

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.

Thanks

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Winston
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Phluphy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 14852
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
triplet.png
[Thumbnail for triplet.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 14852
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashish, my method is definitely not working D:
Guess I'll take a closer look at it tomorrow.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 77906
373
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally got it

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

 
reply
    Bookmark Topic Watch Topic
  • New Topic