Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Sudoku Solver

Ranch Hand
Posts: 143
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 ]

lowercase baba
Posts: 12893
63
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.

Bartender
Posts: 1197
22
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.

Ranch Hand
Posts: 150

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!

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.