Cod sursa(job #1779198)

Utilizator vladbatalanBatalan Vlad vladbatalan Data 14 octombrie 2016 22:19:55
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>

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

int n,sol=0, D[15];
bool col[15], d1[32], d2[32];
bool afisat=0;


void cit()
{
    fin>>n;
}

void afiseaza()
{
    for(int i=1;i<=n;i++)
    {
        fout<<D[i];
        if(i<n)
            fout<<' ';
    }
    fout<<'\n';
}

bool valid(int x, int y)
{
    if(col[y]==1 || d1[x-y+n]==1 || d2[x+y-1]==1)
        return 0;
    return 1;
}

inline void fix(int x, int y)
{
    col[y]=d1[x-y+n]=d2[x+y-1]=1;
    D[x]=y;
}

inline void revin(int x, int y)
{
    col[y]=d1[x-y+n]=d2[x+y-1]=0;D[x]=0;
}

void bk(int index)
{
    if(index==n+1)
    {
        sol++;
        if(!afisat)
        {
            afiseaza();
            afisat=1;
        }
    }
    else
    {
        for(int j=1;j<=n;j++)
            if(valid(index,j))
            {
                fix(index,j);
                bk(index+1);
                revin(index,j);
            }
    }

}

int main()
{
    cit();
    bk(1);
    fout<<sol;
    return 0;
}