Cod sursa(job #1366587)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 1 martie 2015 11:51:38
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include<cstdio>
#include<string>

using namespace std;

#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "damesah";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

int N, nrsol;
int S[27];
int A, B, L;

void ok() {
    int i;

    ++nrsol;

    if(nrsol == 1) {
        for(i = 1; i <= N; ++i)
            printf("%d ", S[i]);
        printf("\n");
    }
}

void back(const int &top) {
    int i;

    for(i = 0; i < N; ++i) {
        if((A & (1 << (top + i))) || (B & (1 << (top - i + N))) || (L & (1 << i)))
            continue;

        S[top] = i + 1;

        A ^= (1 << (top + i));
        B ^= (1 << (top - i + N));
        L ^= (1 << i);

        if(top == N)
            ok();
        else
            back(top + 1);

        A ^= (1 << (top + i));
        B ^= (1 << (top - i + N));
        L ^= (1 << i);
    }
}

int main() {
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);

    scanf("%d", &N);

    back(1);

    printf("%d\n", nrsol);

    return 0;
}