This is my first post to any forum, hope I've posted it in the right place ...............
The following is a program to solve a sudoku, the program has some bugs that I am not able to figure out. I've also give the (error) at the end.................
(It definitely is the most inefficient implementation of Sudoku solver)
For future reference when you have a problem, please ShowSomeEffort and TellTheDetails. It's hard for people to help you if you don't post the code that is troubling you. You may also want to keep your post short, and easy to read; so don't post 500 lines of code, or your complete stack trace.
In this case, I think we can give you a little bit of help with your problem. Stack overflows usually occur when you have an infinite recursive loop. Somewhere in your code, you're calling a method that (in)directly keeps calling itself over and over again. You need to add a condition to it that will make your program exit the method at some point.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Alright, your code tags were positioned wrongly, so I changed them. I also cut off the stack trace to show the lines where it was bubbling up.
Joined: Sep 01, 2011
Thanks very much for making the changes. I do have a termination condition on line 145.I think there might be a logical flaw in my code which I am not able to figure out.
Let me try to explain what I am trying to do.
+ Currently the objective is to solve the Sudoku given by "String input = "360000000004230800000004200070460003820000014500013020001900000007048300000000045"; " (line 64)
+ (ignore line 99 to 102)
+Line 67 to 79 -- we are creating a Cell object assigning a int value from the "input" and then Assigning the Cell to board[i][j] (this is the 9x9 Sudoku board) .
+display() function simply displays the Sudoku board.
+SudokuSolver(int i,int j) solves Sudoku using brute force technique.
+"if (board[i][j].canChange == false)" (line 104) Here we check if the number was given to us as a part of the problem or was it entered by us is any of the previous step.
+"if (firstChangableCell == 0)" (line 105) Here we check and store the co-ordinated of the number which appears before the 1st zero in the input sequence.
+ What we are doing is assigning a the 1st possible value to the cell( as er sudoku rules) then move to the next cell and do the same. If a cell could be assigned any value between 1 to 9, we go back to the previous cell and assign it the 2nd possible value and come back to the cell and try to assign it a value again... We perform this recursively.
+"check(int row, int col, int val)" (line 183) helps us determine if a value could be assigned or no.
+"goback" is used to tell the code whether to traverse back (to change previous value) or forward (to assign new value) on the Sudoku board.
+termination condition (line 145) . while traversing the board it eventually will reach the last point on the board ie board. which will imply the board has been solved. and we can terminate the program.
I hope I have explained it well. I apologize in advance if you find the explanation ambiguous. I'll gladly reexplain the required section.
I just ran your code, it gives me the following output, without an exception:
Joined: Sep 01, 2011
That is just amazing............... What you have got is the perfect ans. I ran the code using both Netbeans and eclipse, and I got the same error (i am still getting the same error i described). What determines the stack size? Is there a way to change it? Do these IDE's place some limitations on stack size? Thanks Stephan. I was really wondering whats wrong with the code logic. Happy to know there is no logical error.
Anyway, there's a lesson in this. Even if the program may run fine on one system, it may fail catastrophically on another.
In an imperative language like Java, it's wise to prefer iteration over recursion. Method calls are more expensive than looping statements, and the stack is relatively small.
Joined: Sep 01, 2011
The following is what i did in eclipse to solve my problem.
1. Go to Run -> RunConfigurations...
2. Select Java Applications -> your_project
3. Select (x)=Arguments in the right panel.
4. Enter "-Xss1024k" (without the quotes) in VM Arguments box.
5. Apply changes and Run.
This increases the stack size and the code runs without and exceptions.
Thanks a lot Stephan for your time and support.
I'll mark this thread as solved. Any other valuable input or idea is always welcomed.