Cod sursa(job #2352640)

Utilizator armandpredaPreda Armand armandpreda Data 23 februarie 2019 15:09:42
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>

using namespace std;

ifstream cin ("damesah.in");
ofstream cout ("damesah.out");

int n, v[15], afis = 0, nr;

int valid(int poz)
{
    int ok=1;
    for(int i=1; i<poz; i++)
    {
        if(v[i]==v[poz] || v[poz]-poz==v[i]-i || v[poz]+poz-n == v[i]+i-n)
            ok=0;
    }
    return ok;
}

void bk(int poz)
{
    if(poz == n+1)
    {
        if(afis == 0)
        {
            for(int i=1; i<=n; i++)
                cout<<v[i]<<' ';
            cout<<'\n';
            afis = 1;
        }
        nr++;
        return;
    }
    for(int i=1; i<=n; i++)
    {
        v[poz]=i;
        if(valid(poz))
        {
            bk(poz+1);
        }
    }
}

int main()
{
    cin>>n;
    bk(1);
    cout << nr;
    return 0;
}