hello-algo/en/codes/c/chapter_backtracking/permutations_i.c

80 lines
2.2 KiB
C

/**
* File: permutations_i.c
* Created Time: 2023-06-04
* Author: Gonglja (glj0@outlook.com), krahets (krahets@163.com)
*/
#include "../utils/common.h"
// Assume a maximum of 1000 permutations
#define MAX_SIZE 1000
/* Backtracking algorithm: Permutation I */
void backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {
// When the state length equals the number of elements, record the solution
if (stateSize == choicesSize) {
res[*resSize] = (int *)malloc(choicesSize * sizeof(int));
for (int i = 0; i < choicesSize; i++) {
res[*resSize][i] = state[i];
}
(*resSize)++;
return;
}
// Traverse all choices
for (int i = 0; i < choicesSize; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements
if (!selected[i]) {
// Attempt: make a choice, update the state
selected[i] = true;
state[stateSize] = choice;
// Proceed to the next round of selection
backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);
// Retract: undo the choice, restore to the previous state
selected[i] = false;
}
}
}
/* Permutation I */
int **permutationsI(int *nums, int numsSize, int *returnSize) {
int *state = (int *)malloc(numsSize * sizeof(int));
bool *selected = (bool *)malloc(numsSize * sizeof(bool));
for (int i = 0; i < numsSize; i++) {
selected[i] = false;
}
int **res = (int **)malloc(MAX_SIZE * sizeof(int *));
*returnSize = 0;
backtrack(state, 0, nums, numsSize, selected, res, returnSize);
free(state);
free(selected);
return res;
}
/* Driver Code */
int main() {
int nums[] = {1, 2, 3};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int returnSize;
int **res = permutationsI(nums, numsSize, &returnSize);
printf("Input array nums =");
printArray(nums, numsSize);
printf("\nAll permutations res = \n");
for (int i = 0; i < returnSize; i++) {
printArray(res[i], numsSize);
}
// Free memory
for (int i = 0; i < returnSize; i++) {
free(res[i]);
}
free(res);
return 0;
}