Cod sursa(job #1401441)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 25 martie 2015 21:28:03
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>
#define nmax 10005
using namespace std;

ifstream fin ("permutari.in");
ofstream fout ("permutari.out");

int n;
bool seen[nmax];
int sol[nmax];

void backtrack (int k){
    if(k==n){
        for(int i=0; i<n; i++) fout << sol[i] << " ";
        fout << "\n";
    }
    for(int i=1; i<=n; i++){
        if(!seen[i]){
            sol[k]= i;
            seen[i]= true;
            backtrack(k+1);
            seen[i]= false;
        }
    }
}

int main(){
    fin.sync_with_stdio(false);
    fin >> n;
    backtrack(0);
    return 0;
}