• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sudoku Solver  RSS feed

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im planning on writing my own sudoku solver, ive got a rough idea of how i can do this. But as you know with sudoku its like.

You get a 3 by 3 box which can have no duplicate numbers int it, and also, no duplicate number on each line. Is there anyway i can represent these 3 by 3 boxes, aswell as being able to read along a whole line? I suppode i could use arrays to make the lines, and then use some sort of loop for checking the boxes in other lines. Argh!!
[ April 04, 2006: Message edited by: Stephen Foy ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might approach it differently... each square with a number is a "cell" each cell belongs to a 3x3 sqaure, a row and a colum. each cell might also belong to the 9x9 "board".

maybe you need to create a class for each of these. each "row" class would have a reference to each cell, as would each column and box. they might actually even all be the same class...

anyway, depending on what you're doing, each time you put a number into a cell, you have to have the row/colum/box that cell belongs to check to see if it's valid (i.e. you're not trying to add a 3 to a row that has a 3).

this is just off the top of my head. it might not work at all, but it might give you some ideas other than using an array.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you think of something as a two-dimensional array of atomic elements, I'm all for representing it as such in program. I'd just make it an int[9][9].

What you might do is have a method that checks for multiple instances of the same digit in a given rectangle of your 9x9 array.

To check the top row, call...
areThereDuplicates(0,0,8,0);

To check the second-to-right column, call...
areThereDuplicates(7,0,7,8);

To check the lower left 3x3 box, call...
areThereDuplicates(0,6,2,8);

Just my two cents.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I might approach it differently... each square with a number is a "cell" each cell belongs to a 3x3 sqaure, a row and a colum. each cell might also belong to the 9x9 "board".

maybe you need to create a class for each of these. each "row" class would have a reference to each cell, as would each column and box. they might actually even all be the same class...

anyway, depending on what you're doing, each time you put a number into a cell, you have to have the row/colum/box that cell belongs to check to see if it's valid (i.e. you're not trying to add a 3 to a row that has a 3).

this is just off the top of my head. it might not work at all, but it might give you some ideas other than using an array.


And over 5 years after writing it, this advice really helped me out! Thanks! Great post Fred!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!