Last active
August 29, 2015 14:19
-
-
Save Dascr32/653579118258491b5aec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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