School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,441 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,525 people online right now. Registration is fast and FREE... Join Now!




Sudoku Solution

 

Sudoku Solution, Need input for writing program that solves sudoku puzzles

Steve_Schlotterer

21 May, 2009 - 01:16 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 12


My Contributions
I'd like to write a computer program to solve sudoku puzzles for me ;-)

Because I'm too lazy to solve them myself! LOL

Seriously, though, how would you approach writing a program that finds the solution for a 9X9 sudoku puzzle?

Data Structure to use?

Algorithm?

I suppose I could use an array for the data structure. Any better ideas?

As far as the algorithm is concerned, I believe I would need some backtracking code, but that's my only idea so far.

Thinking about implementing this in Python, since I've heard so many good things about it, and would like to learn.

All input is appreciated ;-)



User is offlineProfile CardPM
+Quote Post


William_Wilson

RE: Sudoku Solution

21 May, 2009 - 02:37 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,631



Thanked: 88 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Think about how you solve them manually, then try to convert it to code. Sudoku is a very logical puzzle and can be solved both through trying every combination and logic.

If you search sudoku solver here on DIC there are many hints and solutions for you.
http://www.dreamincode.net/?cx=00087208500...&sa=Go#1357
User is offlineProfile CardPM
+Quote Post

mcginleyr1

RE: Sudoku Solution

4 Jun, 2009 - 09:55 AM
Post #3

New D.I.C Head
*

Joined: 4 Jun, 2009
Posts: 1

Read up on constraint propagation. Many of the "easy" Sudoku puzzle can be solved with straight propagation but harder ones will require back tracking.
User is offlineProfile CardPM
+Quote Post

mostyfriedman

RE: Sudoku Solution

10 Jun, 2009 - 11:26 AM
Post #4

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,148



Thanked: 355 times
Dream Kudos: 600
Expert In: Learning

My Contributions
i would probably do it in Prolog...it can be done in a few lines.
User is online!Profile CardPM
+Quote Post

SwiftStriker00

RE: Sudoku Solution

11 Jun, 2009 - 11:16 AM
Post #5

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 295



Thanked: 18 times
Dream Kudos: 125
My Contributions
Well I had to do this in Assembly for school ( and yes i would rather put my head through a pitchfork before attempting that again). The experience has taught me a few ways to go about this.

1st Way) DFS (Depth First Search) is the algorithm that you would implement. this takes picks a state and chooses a value. based of that it picks the next neighbor and follows that to the end of the puzzle. If it finds a dead end, it will back up and try a different path. This algorithm is a fairly standard searching algorithm paired with BFS (searches differently). Assembly doesn't make it easy to implement this algorithm, but its a snap in higher level languages

2nd way) Taking the concept of the first you can start at pos(0,0) and give it a value of one. now move across row and increment its value by 1 at each step At this step check the column Was the input valid? if not keep incrementing until you try them all move back (explain below what that entails). do this to the end of the row. check the row for legitimacy, and if it isnt move back. What i mean by moving back, is to clear the pos you were at (back to 1) and then increment by 1 the pos you move to. This pattern will eventually fill up the table.
Special Conditions:
fixed num: you will have to create a hopping method to jump over that going forward and back
move back at pos(0,0) this means that there were no solutions, and will have to report such when you encounter said event
move forward at pos(maxRow, maxCol): this means puzzle is solved.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:23AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month