Cod sursa(job #3244423)

Utilizator andrei.nNemtisor Andrei andrei.n Data 24 septembrie 2024 19:31:41
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

int v[10],fr[10],n;
ofstream *file;

void backtraking(int x)
{
    if(x == n+1)
    {
        for(int i=1; i<=n; ++i) *file<<v[i]<<' ';
        *file<<'\n';
    }
    else
    {
        for(int i=1; i<=n; ++i)
        {
            if(!fr[i])
            {
                fr[i] = 1;
                v[x] = i;
                backtraking(x+1);
                fr[i] = 0;
            }
        }
    }
}

int main()
{
    ifstream fin ("permutari.in");
    ofstream fout ("permutari.out");
    ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
    fin>>n;
    file = &fout;
    backtraking(1);
    return 0;
}