Monday, May 24, 2010

How to solve sudoku using "C" language?

hi


any idea?


would it be simple program ( 10 - 15 ) pages





is it easy to write a program in c that solves sudoku boards...


thanks

How to solve sudoku using "C" language?
I think it could range from pretty easy to somewhat difficult depending on how you implement the algorithm.





A simple recursive algorithm could be written to solve it just by trying every different possible combination until it came up with the right answer. This would probably take maybe a page or two of code and very little brain power.





But if you don't like the brute force method, you could implement the algorithm with a little more 'intelligence' to try only the more viable combinations. This probably won't be much longer of a program, but it will be more complex and take more analytical thinking on your behalf.





****Edit****





Well I got a little side tracked from other things and decided to implement a program to solve this problem. I admit its something I've thought about doing for a while, so I figured why not.





Anyway , it was pretty easy; took me about an hour and about 130 lines of code, and works great. It reads the Sudoku board in from a text file, solves it, and then outputs the result to another text file. It uses a recursive algorithm that finds the first empty box on the board, fills it with a valid value, and recursively calls itself to do the same on the next empty box. It will keep going this way until there are no valid values to put in the box, in which case it returns false; or if there are no more empty boxes, in which case it returns true. If false is returned, it will keep trying the next valid value until true is returned or there are no more valid values.





I hope my explanation makes sense; if you sit down and think about it, it's really not that complicated. If you want to see my source code, feel free to email me.
Reply:Like most other problems, a SUDOKU solver can use multiple approaches.


The one which may be appropriate will be Backtracking. You read some notes on how backtracking is done.


The basic concept behind this is,


based on available parameters, you presume some values and when it is found that the values assumed go wrong during the course of solving the problem, you go back one step and try again. In this way, you can bring the solution.


Thank You!!
Reply:I am thinking this would not be a very easy program to write, but that all depends on your level of experience, and your definition of 'easy'. If you think 15 pages of code qualifies as easy, then it may be easy for you. I think it would require very complex logic, similar to that necessary for a chess or checkers game.


No comments:

Post a Comment