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.
The general idea behind my current algorithm is to add all of the variables that are in a sub-region (e.g. a 3×3 box for a 9×9 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.
int incSize = (int)Math.sqrt(svars.length);
ArrayList<Variable> subBox = new ArrayList<Variable>();
for (int ind = 0; ind < incSize; ind++) {
for (int ind2 = 0; ind2 < incSize; ind2++) {
subBox.add(svars[ind][ind2]);
}
}
for (int i = 0; i < subBox.size(); i++) {
for (int j = i + 1; j < subBox.size(); j++) {
NotEqualConstraint row = new NotEqualConstraint(subBox.get(i), subBox.get(j));
constraints.add(row);
}
}
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?
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
Here is the working solution I came up with, for those interested: