title: Sudoku recursion with backtracking
body: <p>I am trying to solve any given sudoku puzzle using a recursive backtracking algorithm.  I'm having two problems with my sudoku solver.  First off, it solves for the puzzle, however it recurses back up and unsolves it in the process  (solves in around 4718 recurses and carries on for another 10000 or so back up for some reason).  The second problem stems from my attempt to fix this.  I use a global matrix <i> solution </i> to hold the solution once I find it, verifying I have found it using an isSolved method that looks like this:  </p>
  
  <pre><code>  public static boolean isSolved(int[][]puzzle){
              for(int i = 0; i&lt;9; i++){  //y rotation
                      for(int j = 0; j&lt;8; j++){
                              if(puzzle[i][j]==0){
                                      return false;
                              }
                      }
              }
              return true;
      }
  </code></pre>
  
  <p>where, in my puzzle, a block with a 0 in it is equivalent of it being empty.  However this seems to also get reset although however I cannot find where this is being reset.  Any pointers or suggestions or pointers at where I should look?</p>
  
  <p>Here is my solve method, I have annotated important lines</p>
  
  <pre><code>  public static int[][] solve(int[][]puzzle, int x, int y){
              System.out.println("RecrDepth:  " + recDepth);
              recDepth++;
              //using backtracking for brute force power of the gods(norse cause they obviously most b.a.
              ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
              //next for both  x and y
              int nextx = getNextx(x);
              int nexty = getNexty(x, y);
              while(puzzle[y][x] != 0){  //progress until blank space
                      x = nextx;
                      y = nexty;
                      if(isSolved(puzzle)){
                              System.out.println("resetting solution improperly");
                              solution = puzzle;
                              return puzzle;
                      }
                      nextx = getNextx(x);
                      nexty = getNexty(x, y);
              }
              for(int i = 1; i&lt;10; i++){
                      if(isTrue(puzzle, y, x, i))  //isTrue checks column, row and box so we dont go down unnecessary paths
                              list.add(i);
              }
              for(int i=0; i&lt;list.size(); i++){  //iterating through options in open squre recursing for each one
                      puzzle[y][x]= list.get(i);
                      if(isSolved(puzzle)){
                              System.out.println("Resetting Solution");  //appears to reset solution here but only once that I see in print out
                              solution = puzzle;
                              return puzzle;
                      }
                      System.out.print("=");
                      puzzle = solve(puzzle, nextx, nexty);
                      puzzle[y][x] = 0;//clear spot upon backtracking THIS WAS WHAT I WAS MISSIN
              }
              return puzzle;
      }
  </code></pre>
  
  <p>Thank you for your time again, the full code and readin file is on github at wechtera/ssolverOO  it is the ssolver.java file and the readin is ssolverin.txt</p>