void recursiveSolve(int depth) { if (depth == 0) { for (unsigned char i = 'a'; i <= 'z'; i++) { plaintext[depth] = (u08b_t)i; checkHash(); } for (unsigned char i = '0'; i <= '9'; i++) { plaintext[depth] = (u08b_t)i; checkHash(); } for (unsigned char i = 'A'; i <= 'Z'; i++) { plaintext[depth] = (u08b_t)i; checkHash(); } } else { for (unsigned char i = 'a'; i <= 'z'; i++) { plaintext[depth] = (u08b_t)i; recursiveSolve(depth-1); } for (unsigned char i = '0'; i <= '9'; i++) { plaintext[depth] = (u08b_t)i; recursiveSolve(depth-1); } for (unsigned char i = 'A'; i <= 'Z'; i++) { plaintext[depth] = (u08b_t)i; recursiveSolve(depth-1); } } }
void recursiveSolve(TreeNode *lexTree, WBCell *cell, int solutionLen, char *solutionSoFar) { // base case - if solution is of correct length and is an actual word, then print it if(strlen(solutionSoFar) == solutionLen){ if(isSolution(lexTree, solutionSoFar)){ printf("%s\n", solutionSoFar); } // else, word isn't long enough yet. Try expanding in all directions } else { int newLetterIndex = strlen(solutionSoFar); for(int i = 0; i < NUM_ADJACENT_CELLS; i++){ WBCell *nextCellPtr = cell->adjacentCells[i]; if(nextCellPtr!=NULL && !nextCellPtr->visited){ solutionSoFar[newLetterIndex]=(*nextCellPtr).value; // if new direction is a valid prefix, mark current cell as visited, // and recurse on next cell with updated solution if(isPrefix(lexTree, solutionSoFar)){ cell->visited = 1; recursiveSolve(lexTree, nextCellPtr, solutionLen, solutionSoFar); } // unwind after returning from recursion cell->visited = 0; solutionSoFar[newLetterIndex] = '\0'; } } } }
int main(int argc, char** argv) { memset(plaintext, 0, sizeof(plaintext)); srand(time(NULL)); // Poor man's alphanumeric random generator for (unsigned int i = RAND; i < DEPTH; i++) { while (!((plaintext[i] <= 'z' && plaintext[i] >= 'a') || (plaintext[i] >= 'A' && plaintext[i] <= 'Z') || (plaintext[i] >= '0' && plaintext[i] <= '9'))) plaintext[i] = rand(); } recursiveSolve(DEPTH-RAND); }
// Solves the wordbrain puzzle. Triggers the first call to recursiveSolve. void solvePuzzle(TreeNode *lexTree, WBCell **cells, int numRows, int numCols, int solutionLen) { //loop throught the double array of cells for(int i = 0; i < numRows; i++){ for(int j = 0; j < numCols; j++){ WBCell *cellPtr = &cells[i][j]; // printCell(cellPtr); // initialize solutionSoFar to be all null except for first character char solutionSoFar[solutionLen+1]; for(int x = 0; x < solutionLen+1; x++){ if(x==0){ solutionSoFar[x]=cellPtr->value; } else { solutionSoFar[x]='\0'; } } // printf("solutionSoFar: %s\n", solutionSoFar); // recursively solve if(cellPtr->value != '-') recursiveSolve(lexTree, cellPtr, solutionLen, solutionSoFar); } } }
pair<Cost, Cost> Solver::binaryChoicePoint(Cluster* cluster, Cost lbgood, Cost cub, int varIndex, Value value) { assert(lbgood < cub); if (ToulBar2::interrupted) throw TimeOut(); Cost clb = cub; TreeDecomposition* td = wcsp->getTreeDec(); assert(wcsp->unassigned(varIndex)); assert(wcsp->canbe(varIndex, value)); bool dichotomic = (ToulBar2::dichotomicBranching && ToulBar2::dichotomicBranchingSize < wcsp->getDomainSize(varIndex)); Value middle = value; bool increasing = true; if (dichotomic) { middle = (wcsp->getInf(varIndex) + wcsp->getSup(varIndex)) / 2; if (value <= middle) increasing = true; else increasing = false; } try { Store::store(); assert(td->getCurrentCluster() == cluster); assert(wcsp->getLb() == cluster->getLbRec()); wcsp->setUb(cub); Cost bestlb = lbgood; if (CUT(bestlb, cub)) THROWCONTRADICTION; if (ToulBar2::btdMode >= 2) { Cost rds = td->getLbRecRDS(); bestlb = MAX(bestlb, rds); if (CUT(bestlb, cub)) THROWCONTRADICTION; } lastConflictVar = varIndex; if (dichotomic) { if (increasing) decrease(varIndex, middle); else increase(varIndex, middle + 1); } else assign(varIndex, value); lastConflictVar = -1; bestlb = MAX(bestlb, wcsp->getLb()); pair<Cost, Cost> res = recursiveSolve(cluster, bestlb, cub); clb = MIN(res.first, clb); cub = MIN(res.second, cub); } catch (Contradiction) { wcsp->whenContradiction(); } Store::restore(); nbBacktracks++; if (ToulBar2::restart > 0 && nbBacktracks > nbBacktracksLimit) throw NbBacktracksOut(); #ifdef OPENMPI if (ToulBar2::vnsParallel && ((nbBacktracks % 128) == 0) && MPI_interrupted()) throw TimeOut(); #endif cluster->nbBacktracks++; try { Store::store(); assert(wcsp->getTreeDec()->getCurrentCluster() == cluster); assert(wcsp->getLb() == cluster->getLbRec()); wcsp->setUb(cub); Cost bestlb = lbgood; if (CUT(bestlb, cub)) THROWCONTRADICTION; if (ToulBar2::btdMode >= 2) { Cost rds = td->getLbRecRDS(); bestlb = MAX(bestlb, rds); if (CUT(bestlb, cub)) THROWCONTRADICTION; } if (dichotomic) { if (increasing) increase(varIndex, middle + 1, cluster->nbBacktracks >= cluster->hbfsLimit || nbBacktracks >= cluster->hbfsGlobalLimit); else decrease(varIndex, middle, cluster->nbBacktracks >= cluster->hbfsLimit || nbBacktracks >= cluster->hbfsGlobalLimit); } else remove(varIndex, value, cluster->nbBacktracks >= cluster->hbfsLimit || nbBacktracks >= cluster->hbfsGlobalLimit); bestlb = MAX(bestlb, wcsp->getLb()); if (!ToulBar2::hbfs && cluster == td->getRoot() && initialDepth + 1 == Store::getDepth()) { initialDepth++; showGap(bestlb, cub); }; if (cluster->nbBacktracks >= cluster->hbfsLimit || nbBacktracks >= cluster->hbfsGlobalLimit) { addOpenNode(*(cluster->cp), *(cluster->open), bestlb, cluster->getCurrentDelta()); } else { pair<Cost, Cost> res = recursiveSolve(cluster, bestlb, cub); clb = MIN(res.first, clb); cub = MIN(res.second, cub); } } catch (Contradiction) { wcsp->whenContradiction(); } Store::restore(); assert(lbgood <= clb); assert(clb <= cub); return make_pair(clb, cub); }