// Permutaciones de "abc": en cada posición se elige un elemento que no se haya usado.
#include <stdio.h>
#include <string.h>

#define MAX_ITEMS 16

static void permutations(const char *items, int n, char *current, int *used, int depth,
                         long *nodes) {
    (*nodes)++;
    if (depth == n) {
        current[n] = '\0';
        printf("%s\n", current);
        return;
    }
    for (int i = 0; i < n; i++) {
        if (!used[i]) {
            used[i] = 1;                                                // elegir
            current[depth] = items[i];
            permutations(items, n, current, used, depth + 1, nodes);    // explorar
            used[i] = 0;                                                // deshacer
        }
    }
}

int main(void) {
    const char *items = "abc";
    char current[MAX_ITEMS + 1];
    int used[MAX_ITEMS] = {0};
    long nodes = 0;

    permutations(items, (int) strlen(items), current, used, 0, &nodes);
    printf("nodos: %ld\n", nodes);
    return 0;
}
