Cod sursa(job #1755220)

Utilizator AhileGigel Frone Ahile Data 9 septembrie 2016 16:45:22
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<bits/stdc++.h>
#define in f
#define out g
using namespace std;

#define FIN "permutari.in"

ifstream f("permutari.in");
ofstream g("permutari.out");

int n;
int aux;
int st[1000];

int tipar(int top) {

    for(int i = 1; i <= n; ++i) {
        out << st[i] << " ";
    }
    out << endl;
}

int succesor(int top) {

    if(st[top] < n) {
        st[top] += 1;
        return 1;
    } else {
        return 0;
    }

}

int valid(int top) {
    for(int i = 1; i < top; ++i) {
        if(st[top] == st[i]) {
            return 0;
        }
    }
    return 1;
}

int solutie(int top) {

    if(top == n + 1) {
        return 1;
    } else {
        return 0;
    }

}

int init(int top) {

    st[top] = 0;
    return 0;
}

int bktr(int top) {

    if(solutie(top)){
        tipar(top);
    } else {
        while(succesor(top)) {
            if(valid(top)) {
                init(top + 1);
                bktr(top + 1);
            }
        }
    }

}

int main() {

    freopen(FIN, "r", stdin);
    scanf("%d", &n);
    int top = 1;
    bktr(top);
}