Note that there are some explanatory texts on larger screens.

plurals
  1. POSudoku solver Recurrence stack with try and fail technique
    text
    copied!<p>I am building a Sudoku solver that use the Try and Fail technique to solve any problem. My algorithm is:</p> <p>1)Update (method that remove any possible value that already given as a final value to element in the same Row, column or squar)</p> <p>2)Get the minimum element that has minimum number of possible values</p> <p>3)start solve assuming the first possible value is the final value</p> <p>4)save the current sate into a stack</p> <p>5)Try to solve</p> <p>5-a)If solved, return </p> <p>5-b)if not solved and with invalid Sudoku, then Pop previous state </p> <p>6)Repeat step 3) for all possible vaues (9)</p> <p>7)Repeat step 2) until the puzzel is solved</p> <p>This is my code </p> <pre><code>Stack&lt;Element[][]&gt; myStack= new Stack&lt;&gt;(); private Element[][] mySudoku; public void solve(){ update();//remove all final values from all possible values for each element if(isSudokuSolved(mySudoku)){ return; } //find a cell that is not confirmed and has the minimal candidates int celli=-1,cellj=-1, p=10; for(int i=0;i&lt;9;i++){ for(int j=0;j&lt;9;j++){ if(mySudoku[i][j].getValue()==0){ if(mySudoku[i][j].getPossibleValues().size()&lt;p){ celli=i; cellj=j; p=mySudoku[i][j].getPossibleValues().size(); } } } } try { for (int c = 0; c &lt; mySudoku[celli][cellj].getPossibleValues().size() - 1; c++) { //save state Element[][] copy=deepCopy(mySudoku);//copy the current state myStack.push(copy); //apply candidate to cell mySudoku[celli][cellj].setValue(mySudoku[celli][cellj].getPossibleValues().get(c)); update();//check is solved if(checkValidInputSudoku(mySudoku)){ solve(); }else{ try { mySudoku = myStack.pop(); } catch (EmptyStackException est) { //do nothing } } } } catch (Exception e) { } //if we have reached here then we are at the last possible value for the candidates so confirm candidate in cell if(celli!=-1 &amp;&amp; cellj!=-1 &amp;&amp; p!=10) {//Some problems happen here "out of Boundry -1 Error" mySudoku[celli][cellj].setValue(mySudoku[celli][cellj].getPossibleValues().get(mySudoku[celli][cellj].getPossibleValues().size()-1)); } }//end of solve method </code></pre> <p>I have spent more than 6 hours trying to find out the problem. I have checked for the Update() method, deepCopy() method and checkValidInputSudoku() method. They all works fine. Thank you in Advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload