Cod sursa(job #3303764)

Utilizator Mocanu_Tudor_CristianMocanu Tudor Cristian Mocanu_Tudor_Cristian Data 17 iulie 2025 18:58:47
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, sol[15], rs;
bool col[15], diag1[30], diag2[30];

void bkt(int k) {
    if(k == n + 1) {
        rs++;
        if(rs == 1) {
            for(int i = 1; i <= n; i++)
                fout << sol[i] << ' ';
            fout << '\n';
        }
        return;
    }

    for(int j = 1; j <= n; j++) {
        if(!col[j] && !diag1[k - j + n] && !diag2[k + j - 1]) {
            sol[k] = j;
            col[j] = diag1[k - j + n] = diag2[k + j - 1] = true;
            bkt(k + 1);
            col[j] = diag1[k - j + n] = diag2[k + j - 1] = false;
        }
    }
}

int main() {
    fin >> n;
    bkt(1);
    fout << rs << '\n';

    return 0;
}