Skip to content

Instantly share code, notes, and snippets.

@Dascr32
Last active August 29, 2015 14:19
Show Gist options
  • Save Dascr32/653579118258491b5aec to your computer and use it in GitHub Desktop.
Save Dascr32/653579118258491b5aec to your computer and use it in GitHub Desktop.
public boolean solve(int row, int col) {
if (row == 9) { // Solved!
return true;
}
if (matrix[row][col] != 0) { // Cell not empty move to the other
if (solve(col == 8 ? row + 1 : row, col == 8 ? 0 : col + 1)) {
return true;
}
}
else {
for (int i = 1; i < 10; i++) {
if (isValid(row, col, i)) {
matrix[row][col] = i; // Try number
if (solve(col == 8 ? row + 1 : row, col == 8 ? 0 : col + 1)) {
return true;
}
else {
matrix[row][col] = 0; // Clean the cell and try again
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment