Initially my algorithm involved testing every number from 1->n of an nxn sudoku but since it couldn't solve 16x16 sudokus (had the algorithm running for 3 hours with no luck) I thought it would be better to choose numbers in a cell based on the most valid and least used number on the board. I think I've figured out most of it, e.g. I made a 2D usage vector (first column is number and second is usage) which I sort everytime I make a change on the board however since I am no longer going through each number in a linear fashion I don't know how to check if a cell has had all possible values tested before popping it (whereas before if a cell had the value n there were definitely no more values to choose and it was an immediate sign to pop). Here's the part with the issues I'm trying to fix:
//I used stack <pair<int, int>> to store the most recent row and column we've placed a value in. The first int is the row and the second is the column
//isAllInvalid() just loops from 1-n to check if there are no possible options in a cell left. I initially thought it was a sufficient condition.
while (isAllInvalid(sudoku, solution.top().first, solution.top().second) /*OR we've already tried all values for a cell*/) {
//looks for where the last entered value occurs in the usage vector
int val = sudoku[solution.top().first][solution.top().second];
for (int i = 0; i < usage.size(); i++) {
if (usage[i][0]==val) {
//pops and sets to 0
sudoku[solution.top().first][solution.top().second] = 0;
solution.pop();
//decrement to reflect decrease in usage
usage[i][1] = usage[i][1] - 1;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
break;
}
}
if (solution.size() == 0) {
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire