title: Two Dimensional Array Constraints: Sudoku
body: <p>I am attempting to solve Sudoku as a constraint satisfaction problem for a homework assignment.  I have already constructed constraints for all the elements in a particular row being distinct, as well as for columns.  I am trying to construct the constraints for the elements in a sub-region being distinct, and I'm running into some trouble.</p>
  
  <p>The general idea behind my current algorithm is to add all of the variables that are in a sub-region (e.g. a 3x3 box for a 9x9 grid) into a list, and then permute all the values in that list to construct NotEqualConstraints between each variable.  The code below works properly for the 1st subregion of a NxN grid, but I am not sure how I should change this to iterate through the rest of the entire grid.</p>
  
  <pre><code>int incSize = (int)Math.sqrt(svars.length);
  
  ArrayList&lt;Variable&gt; subBox = new ArrayList&lt;Variable&gt;();
  
  for (int ind = 0; ind &lt; incSize; ind++) {
  for (int ind2 = 0; ind2 &lt; incSize; ind2++) {
      subBox.add(svars[ind][ind2]);
      }
  }
  
  for (int i = 0; i &lt; subBox.size(); i++) {
  for (int j = i + 1; j &lt; subBox.size(); j++) {
     NotEqualConstraint row = new NotEqualConstraint(subBox.get(i), subBox.get(j));
     constraints.add(row);
     }
  }
  </code></pre>
  
  <p>Can anyone guide me in the right direction about how I can modify the code to hit each subregion and not just the top left one?</p>
  
  <p><strong>edit: I am also open to trying any algorithm that works, it is not necessary to add all of the values to an ArrayList for each sub-region. If you see a better way, please share insight</strong></p>