Cod sursa(job #1714025)

Utilizator RaduXD1Nicolae Radu RaduXD1 Data 7 iunie 2016 09:45:36
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<fstream>
#include<bitset>

using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n,sol;
int x[14];
bitset <14> C;
bitset <30> DS;
bitset <30> DP;

int modul(int nr)
{
    if(nr<0)
        return -nr;
    else
        return nr;
}

int cont(int pas)
{
    for (int i=1 ;i<pas;i++)
        if(x[pas]==x[i]||(pas-i==modul(x[pas]-x[i])))
            return 0;
    return 1;
}

void back(int pas)
{
    if(pas>n)
    {
        sol++;
        if(sol==1)
        {
            for(int i=1;i<=n;i++)
                fout<<x[i]<<" ";
            fout<<"\n";
        }
    }
    else
    {
        for(int i=1;i<=n;i++)
            if(C[i]==0&&DS[i+pas]==0&&DP[i-pas+n]==0)
            {
                C[i]=1;
                DS[i+pas]=1;
                DP[i-pas+n]=1;
                x[pas]=i;
                back(pas+1);
                C[i]=0;
                DS[i+pas]=0;
                DP[i-pas+n]=0;
            }
    }
}

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