Cod sursa(job #3319321)

Utilizator Diaconescu99Mihai Diaconescu Diaconescu99 Data 31 octombrie 2025 17:45:24
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;

int n, v[15];
bool col[15], d1[30], d2[30];
long long sol;
bool gasit = false;
ofstream fout("damesah.out");

void scrie() {
    for(int i = 1; i <= n; i++)
        fout << v[i] << (i == n ? '\n' : ' ');
}

void BT(int i) {
    if(i > n) {
        sol++;
        if(!gasit) {
            scrie();
            gasit = true;
        }
        return;
    }
    for(int j = 1; j <= n; j++) {
        if(!col[j] && !d1[i - j + n] && !d2[i + j]) {
            v[i] = j;
            col[j] = d1[i - j + n] = d2[i + j] = true;
            BT(i + 1);
            col[j] = d1[i - j + n] = d2[i + j] = false;
        }
    }
}

int main() {
    ifstream fin("damesah.in");
    fin >> n;
    BT(1);
    fout << sol << endl;
    return 0;
}