Cod sursa(job #1588635)

Utilizator leopop29Pop Leonard leopop29 Data 3 februarie 2016 13:34:56
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <bitset>

using namespace std;

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

bitset<27> d1;
bitset<127> d2;
bitset<14> c;
int n, sol = 0, v[14];

void bk(int x)
{
    if(x > n)
    {
        ++sol;
    }
    for(int i = 1; i <= n; ++i)
        if(!c[i] && !d1[i+x] && !d2[i-x+100])
        {
            c[i] = 1;
            d1[i+x] = 1;
            d2[i-x+100] = 1;
            if(!sol)
                v[x] = i;
            bk(x+1);
            c[i] = 0;
            d1[i+x] = 0;
            d2[i-x+100] = 0;
        }
}

int main()
{
    f >> n;

    bk(1);

    for(int i = 1; i <= n; ++i)
        g << v[i] << ' ';
    g << '\n';
    g << sol;
    return 0;
}