Skip to content

Instantly share code, notes, and snippets.

@rharriso
Last active May 1, 2017 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rharriso/c56566fc50f73639a7d447e7c16b5d44 to your computer and use it in GitHub Desktop.
Save rharriso/c56566fc50f73639a7d447e7c16b5d44 to your computer and use it in GitHub Desktop.
using cellQueue = std::deque<SudokuCell>;
class SudokuBoard {
/*
* fill the board with valid solution
*/
void fillCells() {
auto remaininCells = cellQueue{cells};
if(!doFillCells(remaininCells)) {
cout << "Unable to fill board" << endl;
}
}
bool doFillCells(cellQueue &remainingCells) {
// get first cell and tail
auto cell = remainingCells.front();
remainingCells.pop_front();
// collected from all the neighbor values
set<int> neighborValues = {};
// produced by set difference VALID_VALUES \ neighbord values
vector<int> options;
for(auto option : options) {
cell->value = option;
// base case (no remaining cells)
if (remainingCells.empty()) {
return true;
}
// recursive step
if (doFillCells(remainingCells)){
return true;
};
}
// base case (push cell back to set, and backtrack)
cell->value = 0;
remainingCells.push_front(cell);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment