Cod sursa(job #1347163)

Utilizator Seb16Ungureanu Paul Sebastian Seb16 Data 18 februarie 2015 20:23:11
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
using namespace std;
ifstream fin("permutari.in");
ofstream fout("permutari.out");
int n, v[100];
bool IsValid(int k)
{
    for(int i=1;i<k;i++)
    {
        if(v[i]==v[k])
        {
            return false;
        }
    }
    return true;
}
bool IsSolution(int k)
{
    return n==k;
}
void afis ()
{
    for(int i=1;i<=n;i++)
    {
        fout<<v[i]<<' ';
    }
    fout<<'\n';
}
void bt(int k)
{
    for(int i=1; i<=n; i++)
    {
        v[k]=i;
        if(IsValid(k))
        {
            if(IsSolution(k))
            {
                afis();
            }
            else
            {
                bt(k+1);
            }
        }

    }
}
int main()
{
    fin>>n;
    bt(1);
    fin.close();
    fout.close();
}
/*
4
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
*/