int main(int argc, const char* argv[]) { int v[] = {0,1,2,3,4,5,6,7,8,9,10,12}; vector<int> cand(v, v+sizeof(v)/sizeof(int)); cout << findMissing(cand) << endl; return 0; }
void RecursiveSolver::solve(Sudoku& sudoku) { this->_field = sudoku.getField(); options = findMissing(); for(map<int,vector<int>>::iterator it = options.begin(); it != options.end(); ++it) { cout << it->first << " " << ": "; for (vector<int>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) { cout << *it2 << ", "; } cout << endl; } map<int,vector<int>>::iterator it = options.begin(); count = 0; int status = -1; do { status = guess(it->first); switch(status) { case RETURN_NEXT: it = std::next(it); break; case RETURN_PREV: it = std::prev(it); break; } } while(status != RETURN_EXIT); sudoku.setField(_field); }
CmCError Run(CmCParser *script) { //define and initialize parameter table void *(paramTable)[PARAM_NUM + 2*CUST_CURVE_NUM]; int i; for(i = 0; i < PARAM_NUM + 2*CUST_CURVE_NUM; i++) paramTable[i] = (void *) NULL; //parse script, setting parameters and //running commands along the way... int tokenId, error; CmCToken *token; while(token = script->GetToken()) { //determine type of token (parameter or command) tokenId = Command(token->token_); //token is a command, so run it if(tokenId != UNKNOWN_COMMAND) { error = EXECUTE(tokenId, script, paramTable); if(error) { if(error == EXE_MISSING_PARAM) { CmCToken *ertok = new CmCToken((char *) PARAM_LIST[findMissing(paramTable, Command(token->token_))], token->lineNumber_); return CmCError(ertok, error); } else { return CmCError(token, error); } } } //token is a parameter, so set its value else { tokenId = Parameter(token->token_); if(tokenId != UNKNOWN_PARAMETER) { error = SET(tokenId, script, paramTable); if(error) return CmCError(token, error); //token is custum curve list, so read it } else { tokenId = CustomCurve(token->token_); error = READLIST(tokenId, script, paramTable); if(error) return CmCError(token, error); } } } //done running script... //de-allocate parameter table DEALLOCATE(paramTable); //reset script parser script->StartOver(); //script has been run succesfully! return CmCError((CmCToken *) NULL, NO_ERRORS); }
int findMissing(vector<int> cand, int col){ if(!cand.size()) return 0; vector<int> zeros, ones; for(int i = 0; i < cand.size(); ++ i){ if(fetchBit(cand[i], col)) ones.push_back(cand[i]); else zeros.push_back(cand[i]); } if(zeros.size() <= ones.size()){ int v = findMissing(zeros, col+1); return (v << 1)|0; }else{ int v = findMissing(ones,col+1); return (v << 1)|1; } return 0; }
int main() { char *str="LiniL"; printf("%d\n",isPalindrome(str)); char *str1="LiiL"; printf("%d\n",isPalindrome(str1)); // int arr[10]={2,3,4,6,7,8,9,10,11,12}; // printf("%d\n", missingNumber(arr, 10)); int arr[2]={1,2}; char *ip1=strdup("1.1.1.12"); char *ip2=strdup("1.1.1.2"); printf("%d\n", compareIps(ip1,ip2)); char *s="Linux"; printReverse(s); printf("\n"); printf("%d\n", findMissing(arr,2)); printf("%d\n", findMissingBinary(arr,2)); }
int findMissing(vector<int> cand){ return findMissing(cand, 0); }
void Solver::solve(Sudoku& sudoku) { this->_field = sudoku.getField(); if (_field.countEmpty() == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 1" << endl; return; } // Look for slots with only one option, fill it in and repeat bool hit; do { hit = false; // Find all possible options for the empty slots missing = findMissing(); // Find all slots with only one option for (int index = 0; index < _field.getSizeSq(); ++index) { if (missing[index].size() == 1) { _field.set(index, missing[index].at(0)); missing[index].clear(); cout << "MADE A HIT AT " << index << " WITH " << _field.get(index) << endl; hit = true; } } } while (hit == true); // We completed the puzzle, thus return if (_field.countEmpty(true) == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 2" << endl; sudoku.setField(_field); return; } // No more sure slots, so start guessing std::cout << "COMPLETED ALL SURE HITS, START GUESSING" << std::endl; // Print the result so far std::cout << "RESULT SO FAR: " << std::endl; Sudoku s = Sudoku(); s.setField(_field); s.print(); // Find all possible options for the remaining empty slots missing = findMissing(); // Print all options for (int index = 0; index < _field.getSizeSq(); ++index) { cout << "[" << index << "]: "; for (int index2 = 0; index2 < missing[index].size(); ++index2) { cout << missing[index].at(index2) << " "; } cout << "\n"; } emptySlots.clear(); // Fill in the empty slots to the array for(map<int,vector<int>>::iterator it = missing.begin(); it != missing.end(); ++it) { if ((it->second).size() > 0) emptySlots.push_back(it->first); } // Sort the emptyslot indices by size small->big std::sort(emptySlots.begin(), emptySlots.end(), lessThan); guess(emptySlots.at(0)); sudoku.setField(_field); }