int ScrambleSolve<size>::solve(BoardAbstract<size>& board) { startTimer(); int solutionCount = backtrackSolve(board); stopTimer(); return solutionCount; }
bool backtrackSolve(bool (*board)[8][8], unsigned short vPos, std::promise<bool>& p) { if(vPos == 8) // catch for already complete board return true; for(unsigned short hPos = 0; hPos < 8; hPos++) if(singleConflictCheck(board, hPos, vPos)) { (*board)[hPos][vPos] = 1; if(backtrackSolve(board, vPos + 1, p) == true) //return chain to escape all recursively called functions. { if(vPos == 0) //only run on initial function call p.set_value_at_thread_exit(true); //setting thread complete status on thread exit return true; } (*board)[hPos][vPos] = 0; } return false; }
int ScrambleSolve<size>::backtrackSolve(BoardAbstract<size>& board) { if(board.isBoardFull()) { return 1; } int solutionsFound = 0; CELL_INDEX pos = board.getFilledCount(); // Get the possible values for the cell board.updateCandidates(pos); BITMASK possible = board.getCandidates(pos); // Choose the order to try values BITMASK cellOrder[dim.UNIT]; for(int i = 0; i < dim.UNIT; i++) { cellOrder[i] = (1 << (i + 1)); } util::ArrayShuffle<BITMASK>(cellOrder, dim.UNIT); // Go through values and recursively try them for(int i = 0; i < dim.UNIT; i++) { if(cellOrder[i] & possible) { board.set(pos, cellOrder[i]); solutionsFound = backtrackSolve(board); if(solutionsFound > 0) { return solutionsFound; } board.remove(pos); possible &= ~cellOrder[i]; } } return 0; }