Cod sursa(job #1901029)

Utilizator matystroiaStroia Matei matystroia Data 3 martie 2017 18:32:00
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
using namespace std;

ifstream fin("damesah.in");
ofstream fout("damesah.out");

const int N=15;
int n, st[N], sol;
bool found;

bool valid(int k)
{
    for(int i=1;i<k;++i)
    {
        if(i+st[i]==k+st[k])
            return false;
        if(i-st[i]==k-st[k])
            return false;
        if(st[i]==st[k])
            return false;
    }
    return true;
}

void afis()
{
    for(int i=1;i<=n;++i)
        fout<<st[i]<<" ";
}

void bk(int k)
{
    for(st[k]=1;st[k]<=n;++st[k])
    {
        if(valid(k))
        {
            if(k==n)
            {
                if(!found)afis(), found=true;
                sol++;
            }
            bk(k+1);
        }
    }
}

int main()
{
    fin>>n;
    bk(1);
    fout<<'\n'<<sol;
    return 0;
}