Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

Basic Approach

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Setting sudoku
View previous topic :: View next topic  
Author Message
King Wonka

Joined: 14 Dec 2005
Posts: 26
:

Items
PostPosted: Mon Dec 19, 2005 8:31 pm    Post subject: Basic Approach Reply with quote

How does this sound for just a pure generator in pseudo-code. I really don't want to re-use any code here but advice is welcome. I have a general idea for creating a sudoku board but but it doesn't quite get one finished.

1. set up a Grid[9,9] array, Row[9,10],Col[9,10],Box[9,10]
2. select 3 random numbers (row=0 thru 8, col=0 thru 8, n=1 thru 9)
3. check to see if the cell is unused
4. check to see if the number already exists in row, col, or box
5. if cell is empty and it doesn't exist in row,col, or box then plug in the number
6. if it does exist, cross it off the list for those row,col,box and set Grid[r,c]=0
7. repeat until total count of grid[r,c]=81

This sounds ok on paper but it's getting the grid about 70% done and starts over because it runs out of options. What can I do to keep it simple but make it do what it's supposed to? Thanks


Anthony
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Mon Dec 19, 2005 10:08 pm    Post subject: Reply with quote

Hi Anthony,

been there, done that.

It does fail because you are just using only the basic rules to check if a cell is valid. This is fine for the first few digits, but after a while, the depencencies start to work and you are filling digits that should not go there. One normally uses a backtracking solver to check whether there is still a valid solution, then remove the digit when that is not the case.

There is a way to bypass this, that is when you start counting the number of digits that can still go into a cell. When you give priority to cells that have the least number of candidates, you will see that the program does not fail so often.

Ruud.
_________________
Meet me at sudocue.net
Back to top
View user's profile Send private message Visit poster's website
King Wonka

Joined: 14 Dec 2005
Posts: 26
:

Items
PostPosted: Tue Dec 20, 2005 8:41 pm    Post subject: Reply with quote

Well I got the generator working and didn't use any code from here! That was my goal. Here's how it works:

1. make the grid[9,9]
2. set up arrays for Row and Col to store what's been used where(didn't use box and it's just fine)
3. break the grid up into nine 3x3 mini grids
4. start placing 1s first...one in each mini grid
5. then do 2s, 3s, etc placing all 9 of each number at once
6. if there's a conflict...delete the current number and previous number from the grid and start again with the previous number
7. when n=10 then we have a successful sudoku

Cranks em out at about 1 every 20-50ms. I've seen faster generators but this will do nicely (plus I'm proud of it Very Happy )

Now to move onto to a solver so I can start removing clues and doing symmetry. Twisted Evil
Back to top
View user's profile Send private message
King Wonka

Joined: 14 Dec 2005
Posts: 26
:

Items
PostPosted: Thu Dec 22, 2005 6:29 pm    Post subject: Reply with quote

Here's what my generator is putting out now. Symmetry is in...the basic rules are in. So how do these look?

Code:

Sudoku #1
. 2 . | . . 7 | 4 . .
1 5 . | . . 3 | 2 . .
8 . . | . . . | 7 . 3
------+-------+------
. . . | . . 8 | 6 7 4
5 . . | 1 4 9 | . . 8
3 7 8 | 2 . . | . . .
------+-------+------
2 . 7 | . . . | . . 1
. . 6 | 9 . . | . 5 2
. . 5 | 3 . . | . 8 .

Sudoku #2
. 2 . | . . . | 5 . .
. . . | 5 2 . | 9 . 7
1 . 3 | 4 7 . | 8 . .
------+-------+------
. . 2 | 6 4 . | . . .
3 6 . | . 9 . | . 4 1
. . . | . 5 2 | 6 . .
------+-------+------
. . 1 | . 6 7 | 2 . 3
6 . 4 | . 2 1 | . . .
. . 8 | . . . | . 9 .

Sudoku #3
3 . 9 | . . . | 6 4 .
. 1 . | 2 5 9 | . . 8
. . 4 | . . 7 | . . .
------+-------+------
. . 3 | . . 5 | . 1 .
9 7 . | . 2 . | . 3 6
. 8 . | 7 . . | 5 . .
------+-------+------
. . . | 6 . . | 2 . .
6 . . | 4 5 8 | . 7 .
. 3 5 | . . . | 9 . 4

Sudoku #4
3 9 . | 6 1 . | . . .
. . 4 | 5 3 . | . . .
2 . . | . . . | . 1 6
------+-------+------
. 6 8 | . . . | 5 . .
. 2 7 | 1 8 5 | 3 6 .
. . 1 | . . . | 9 2 .
------+-------+------
1 4 . | . . . | . . 3
. . . | . 6 7 | 8 . .
. . . | . 5 2 | . 7 1

Sudoku #5
3 . 9 | 1 8 . | . 7 .
. . . | 7 . . | 1 5 8
5 . . | 4 . . | . 3 .
------+-------+------
. 1 4 | . . 5 | . . .
2 . . | . 3 . | . . 4
. . . | 8 . . | 3 2 .
------+-------+------
. 5 . | . . 2 | . . 3
6 2 8 | . . 7 | . . .
. 7 . | . 4 9 | 8 . 5
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Thu Dec 22, 2005 10:40 pm    Post subject: Reply with quote

The basic rules say:



There should be a 7 there, right?

The other 4 also fail at the basic rules.

You need to check remaining candidates, Anthony.

Ruud.
_________________
Meet me at sudocue.net
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Setting sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron