By saurabhv
via programminggeeks.com
Published: May 10 2010 / 02:53
Developing a Sudoku Solver program is perhaps the the only thing which is more interesting than solving a Sudoku puzzle. :)
The java code given below here the puzzles in 4 steps.



Comments
zdbrig replied ago:
with this matrix (taken form gnome sudoku) , this does not work:
static int newa[][]=
{{3,0,0,5,0,8,0,0,1},
{4,0,2,0,0,1,8,5,0},
{0,1,8,6,0,0,0,0,7},
{0,0,5,0,0,3,4,0,0},
{0,8,0,0,0,0,0,2,0},
{0,0,1,2,0,0,6,0,0},
{2,0,0,0,0,5,9,3,0},
{0,5,3,4,0,0,1,0,2},
{1,0,0,3,0,2,0,0,4}};
saurabhv replied ago:
This sudoku solver solved your puzzle upto here :
3 0 0 5 0 8 2 0 1
4 0 2 0 3 1 8 5 0
5 1 8 6 2 0 3 0 7
0 2 5 0 0 3 4 1 0
0 8 4 1 5 0 7 2 3
0 3 1 2 0 0 6 0 5
2 4 0 0 1 5 9 3 0
8 5 3 4 0 0 1 0 2
1 0 0 3 0 2 5 0 4
I cant see any logical way (without guess) to proceed. Kindly let me know if you could find one.
dr_jerry replied ago:
Well you have to guess and apply recursive programming if your reasoning fails. You should maintain a list with all posible values per field. Pick a field with the least number of possible values and iterater over that, until you have
1) a successfull match.
2) a guaranteed fail.
3) another cant proceed situation, in which case you should backtrack or start guessing again.
If your code would have been more modular it would be possible to advise but I'm afraid it stops here
zynasis replied ago:
horrible code
dr_jerry replied ago:
Horrible code indeed don't take this as an example for learning java.
saurabhv replied ago:
Indeed it's not about learning Java. It's about your science of reasoning ;)
Dave Newton replied ago:
Turning code on its side is *not* a graph of how awesome it is. Nested, what, 11 blocks deep at one point?
*shudder*
saurabhv replied ago:
I haven't used any data structure, not even TREE, so that even a kid of standard 7th or 8th, who doesn't have much knowledge about data structure but has exellent logic can understand and manipulate the code.
eelmore replied ago:
This code is a crime against humanity and a holocaust of logic. What has been seen cannot be unseen! A pox on your house!
Travis Calder replied ago:
I'm sorry, but what?!
You think a kid of standard 7th or 8th (I assume you mean grade?) can understand control-blocks nested 11 levels deep?
But you don't think they know what a 2 dimensional cartesian point is?
I spent a portion of last night attempting a refactor for a blog post, and I have to say... abstract variable names, super deep control structures, and almost no real level of abstraction... I'm a professional programmer, and I have no CLUE what some of your loops are attempting. Yet. I will, but they're that hard.
Dave Newton replied ago:
I apologize, but there's no excuse for this code. This is *substantially* more difficult to understand than a properly-refactored implementation, even if the logic itself stays exactly the same. The number of moving parts may be the same--but the cognitive overhead of mashing it all into one gigantic pile of yuck is detrimental to understanding.
Voters For This Link (7)
Voters Against This Link (7)