Cod sursa(job #1860610)

Utilizator roxannemafteiuMafteiu-Scai Roxana roxannemafteiu Data 28 ianuarie 2017 11:08:05
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

const short N_MAX = 15;

short n, answer;
vector <short> queen(N_MAX);
bitset <N_MAX> column;
bitset <N_MAX * 2> main_diag, secondary_diag;

inline void backt(short level) {
    if (level == n + 1) {
        if (answer < 1) {
            for (int i = 1; i <= n; ++i) {
                fout << queen[i] << ' ';
            }

            fout << '\n';
        }
        ++answer;
    } else {
        for (int i = 1; i <= n; ++i) {
            if (!column[i] && !main_diag[i + n - level] && !secondary_diag[level + i]) {
                queen[level] = i;

                column.set(i); main_diag.set(i + n - level); secondary_diag.set(level + i);
                backt(level + 1);
                column.reset(i); main_diag.reset(i + n - level); secondary_diag.reset(level + i);
            }
        }
    }
}

int main() {
    fin >> n;

    backt(1);

    fout << answer;

    return 0;
}