Cod sursa(job #2241713)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 16 septembrie 2018 19:42:02
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("permutari.in");
ofstream fout("permutari.out");
int st[20] , n;
void Afisare(int top)
{
    int i;
    for(i = 1; i <= top; i++)
        fout << st[i] << " ";
    fout << "\n";
}
int Validare(int top)
{
    int i;
    for(i = 1; i < top; i++)
        if(st[i] == st[top])return 0;
    return 1;
}
void Backtracking()
{
    int cand , top;
    top = 1;
    st[top] = 0;
    while(top > 0)
    {
        cand = 0;
        while(cand == 0 && st[top] < n)
        {
            st[top]++;
            cand = Validare(top);
        }
        if(cand == 0)top--;
            else if(top == n)Afisare(top);
                else st[++top] = 0;
    }
}
int main()
{
    fin >> n;
    Backtracking();
    return 0;
}