Cod sursa(job #2386443)

Utilizator GabyD002Dobrita Gabriel GabyD002 Data 22 martie 2019 22:51:00
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
#define NM 10
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
int n,top,st[NM];
void Write()
{   for(int i=1; i<=n; i++) g<<st[i]<<' ';
    g<<'\n';
}
bool Valid()
{   for(int i=1; i<top; i++)
        if(st[i]==st[top]) return 0;
    return 1;
}
void BKT()
{   st[++top]=0;
    while(top)
    {   bool ok=0;
        while(!ok && st[top]<n)
        {   st[top]++;
            ok=Valid();
        }
        if(!ok) top--;
            else
                if(top==n) Write();
                    else st[++top]=0;
    }
}
int main()
{   f>>n;
    BKT();
    return 0;
}