BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Scrum
  2. Apache Maven 2
  3. Essential MySQL
  4. Node.js
  5. Groovy
  1. jQuery Selectors
  2. Ajax
  3. Java
  4. Spring Config.
  5. Java Concurrency

Link Details

Link 410575 thumbnail
User 748747 avatar

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.
  • 7
  • 7
  • 2102
  • 2

Comments

Add your comment
User 507741 avatar

zdbrig replied ago:

0 votes Vote down Vote up Reply

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}};

User 748747 avatar

saurabhv replied ago:

0 votes Vote down Vote up Reply

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.

User 363077 avatar

dr_jerry replied ago:

0 votes Vote down Vote up Reply

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

User 211643 avatar

zynasis replied ago:

0 votes Vote down Vote up Reply

horrible code

User 363077 avatar

dr_jerry replied ago:

0 votes Vote down Vote up Reply

Horrible code indeed don't take this as an example for learning java.

User 748747 avatar

saurabhv replied ago:

0 votes Vote down Vote up Reply

Indeed it's not about learning Java. It's about your science of reasoning ;)

User 306525 avatar

Dave Newton replied ago:

0 votes Vote down Vote up Reply

Turning code on its side is *not* a graph of how awesome it is. Nested, what, 11 blocks deep at one point?

*shudder*

User 748747 avatar

saurabhv replied ago:

0 votes Vote down Vote up Reply

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.

User 218789 avatar

eelmore replied ago:

2 votes Vote down Vote up Reply

This code is a crime against humanity and a holocaust of logic. What has been seen cannot be unseen! A pox on your house!

User 745365 avatar

Travis Calder replied ago:

0 votes Vote down Vote up Reply

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.

User 306525 avatar

Dave Newton replied ago:

0 votes Vote down Vote up Reply

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.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.