Cod sursa(job #1344321)

Utilizator ThomasFMI Suditu Thomas Thomas Data 16 februarie 2015 17:06:46
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
using namespace std;

#define NMax 15

ifstream f("damesah.in");
ofstream g("damesah.out");

int n, N;

int sol[NMax];
bool col[NMax],d1[2*NMax],d2[2*NMax];

bool ok;
int nrsol;

void bkt(int k)
{
    if(k == n)
    {
        if(!ok)
        {
            for(int i=0;i<n;++i) g<<sol[i]+1<<" "; g<<"\n";
            ok = true;
        }
        nrsol++;
        return;
    }

    for(int i=0;i<n;++i)
    {
        int nr1 = i-k;
        int nr2 = n-i-1-k;

        if(nr1 < 0) nr1 += N;
        if(nr2 < 0) nr2 += N;

        if(!col[i] && !d1[nr1] && !d2[nr2])
        {
            sol[k] = i;
            col[i] = d1[nr1] = d2[nr2] = true;
            bkt(k+1);
            col[i] = d1[nr1] = d2[nr2] = false;
        }
    }
}

int main()
{
    f>>n;
    N = 2*n;
    bkt(0);

    g<<nrsol<<"\n";

    f.close();
    g.close();
    return 0;
}