Cod sursa(job #2801129)

Utilizator Tudor06MusatTudor Tudor06 Data 15 noiembrie 2021 00:24:13
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>

using namespace std;

const int NMAX = 13;

bool col[NMAX + 1];
bool diag1[2 * NMAX + 1];
bool diag2[2 * NMAX + 1];

int cnt;
int afisat;
int n;

int ans[NMAX + 1];

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

void bkt( int lin ) {
    if ( lin == n + 1 ) {
        cnt ++;
        if ( !afisat ) {
            for ( int i = 1; i <= n; i ++ ) {
                fout << ans[i] << ' ';
            }
            afisat = 1;
        }
    }
    for ( int c = 1; c <= n; c ++ ) {
        int d1 = lin + c;
        int d2 = NMAX + lin - c;
        if ( !col[c] && !diag1[d1] && !diag2[d2] ) {
            col[c]    = 1;
            diag1[d1] = 1;
            diag2[d2] = 1;

            ans[lin] = c;
            bkt( lin + 1 );

            col[c]    = 0;
            diag1[d1] = 0;
            diag2[d2] = 0;
        }
    }
}
int main() {
    fin >> n;
    bkt( 1 );
    fout << '\n' << cnt;
    return 0;
}