void generate_combinations(vector<bool>& used, int start, int k, vector<vector<int>>& result) { if (k == 0) { vector<int> found_combination; for (unsigned int i = 0; i < used.size(); i++) { if (used[i]) { found_combination.push_back(i + 1); } } result.push_back(found_combination); } else { for (unsigned int i = start; i < used.size(); i++) { if (!used[i]) { used[i] = true; generate_combinations(used, i + 1, k - 1, result); used[i] = false; } } } }
/* * generator_init * initialize the generator of combinations * * The generator produces combinations of K elements in the interval (0..N). * We prebuild all the combinations in this method, which is simpler than * generating them on the fly. */ static CombinationGenerator * generator_init(int n, int k) { CombinationGenerator *state; Assert((n >= k) && (k > 0)); /* allocate the generator state as a single chunk of memory */ state = (CombinationGenerator *) palloc(sizeof(CombinationGenerator)); state->ncombinations = n_choose_k(n, k); /* pre-allocate space for all combinations */ state->combinations = (int *) palloc(sizeof(int) * k * state->ncombinations); state->current = 0; state->k = k; state->n = n; /* now actually pre-generate all the combinations of K elements */ generate_combinations(state); /* make sure we got the expected number of combinations */ Assert(state->current == state->ncombinations); /* reset the number, so we start with the first one */ state->current = 0; return state; }
vector<vector<int>> combine(int n, int k) { vector<vector<int>> result; vector<bool> used; used.resize(n); for (int i = 0; i < n; i++) { used[i] = false; } generate_combinations(used, 0, k, result); return result; }
vector<vector<int> > combine(int n, int k) { vector<vector<int> > combinations; vector<int> combination; vector<int> numbers; for (int i = 1; i <= n; ++i) { numbers.push_back(i); } generate_combinations(numbers, 0, k, combination, combinations); return combinations; }
int main () { int positions = 2; int combo[positions]; int set_size = 8; int num_set[set_size]; int i; for (i = 0; i < set_size; i++) { num_set[i] = i; } printf ("generate combination of octal numbers\n"); generate_combinations (positions, combo, set_size, num_set); positions = 3; set_size = 4; int second_combo[positions]; int letter_set[set_size]; for (i = 0; i < set_size; i++) { letter_set[i] = 'A' + i; } printf ("\ngenerate padlock combinations when it is 4 wheels of A to C\n"); generate_combinations (positions, second_combo, set_size, letter_set); for (i = 0; i < set_size; i++) { letter_set[i] = 'Q' + i; } printf ("\ngenerate padlock combinations iteratively\n"); iterative_combinations (set_size, letter_set, positions); return 0; }
void generate_combinations(vector<int> &numbers, int index, int count, vector<int> &combination, vector<vector<int> > &combinations) { if (0 == count) { combinations.push_back(combination); } else { for (int i = index; i <= (numbers.size() - count); ++i) { combination.push_back(numbers[i]); generate_combinations(numbers, i + 1, count - 1, combination, combinations); combination.pop_back(); } } }
void backtrack(char *arr, int len, char *curptr, int curlen) { int c[2]; char temp; int combinations=0; if(accept(curlen, len)) { print(arr, len); } else { combinations = generate_combinations(arr, c); for (int i=0;i<combinations;i++) { move(arr,c[i],&temp,curlen); backtrack(arr,len,curptr+1,curlen+1); unmove(arr,c[i],temp,curlen); } } }