Cod sursa(job #1779299)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 15 octombrie 2016 01:10:53
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int V[20], N, nrSol;

int Conditie(int k){
    for(int i=1; i<k; ++i)
        if(V[i] == V[k]) return 0;
    return 1;
}

int Succesor(int k){
    if(V[k] < N){
        V[k]++;
        return 1;
    }
    else return 0;
}

void Afisare(){
    for(int i=1; i<=N; ++i)
        g << V[i] << ' ';
    g << '\n';
}

void BKTR(int k){
    V[k] = 0;
    while(Succesor(k)){
        if(Conditie(k))
            if(k==N) Afisare(), nrSol++;
        else BKTR(k+1);
    }
}

int main(){
    f >> N;
    BKTR(1);
    return 0;
}