Cod sursa(job #2605947)

Utilizator MerlinTheWizardMelvin Abibula MerlinTheWizard Data 26 aprilie 2020 14:16:53
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int st[101],cnt,n,ok1;
void afisare()
{
    for(int i=1;i<=n;i++)
        g<<st[i]<<" ";
    g<<"\n";
    ok1++;
}
int Validare(int top,int i)
{
    int ok=1;
    for(int j=1;j<=top-1;j++)
    {
        if(st[j]==i || abs(top-j)==abs(i-st[j]))
            ok=0;
    }
    if(ok==1)
        return 1;
    else
        return 0;
}
void bkt(int top)
{
    if(top==n+1)
    {
        cnt++;
        if(ok1!=1)
            afisare();
    }
    else
    {
        for(int i=1;i<=n;i++)
            if(Validare(top,i))
            {
                st[top]=i;
                bkt(top+1);
            }
    }
}
int main()
{
    f>>n;
    bkt(1);
    g<<cnt;
}