Cod sursa(job #3192821)

Utilizator Ilinca_Radu_2022Radu Ilinca-Rucsandra Ilinca_Radu_2022 Data 13 ianuarie 2024 11:55:51
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define C 20
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, dp[2*C], ds[2*C], markcol[C], sol[C], nr;
void bkt(int l) {
    if (l>n) {
        nr++;
        if (nr==1) {
            for (int i=1; i<=n; i++) fout<<sol[i]<<' ';
            fout<<'\n';
        }
    }
    else {
        for (int c=1; c<=n; c++) {
            int DS=l+c;
            int DP=l-c+n;
            if (ds[DS]==0 && markcol[c]==0 && dp[DP]==0) {
                sol[l]=c;
                ds[DS]=1;
                markcol[c]=1;
                dp[DP]=1;
                bkt(l+1);
                ds[DS]=0;
                markcol[c]=0;
                dp[DP]=0;
            }
        }
    }
}
int main()
{
    fin>>n;
    bkt(1);
    fout<<nr;
    return 0;
}