Cod sursa(job #1074840)

Utilizator andrei31Andrei Datcu andrei31 Data 7 ianuarie 2014 23:44:15
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <vector>
#include <fstream>

void printSolution (const std::vector<int> &sol){
    static std::ofstream fout("permutari.out");

    for (int a : sol)
        fout << a + 1 << ' ';
    fout << std::endl;
}

void back(int depth, int n){
    static std::vector<bool> used(n, false);
    static std::vector<int> solution(n);

    for (int i = 0; i < n; ++i)
        if (!used[i]){

            used[i] = true;
            solution[depth] = i;

            if (depth == n - 1)
                printSolution(solution);
            else
                back(depth + 1, n);
            used[i] = false;
        }
}

int main(){

    std::ifstream fin("permutari.in");
    int n;
    fin >> n;
    back(0, n);
    return 0;
}