Cod sursa(job #3364047)

Utilizator Alias47John Doe Alias47 Data 28 august 2026 11:50:13
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
#define ft(n) for(int i=1; i<=n; i++)
#define sp ' '
string file = "damesah";
ifstream f(file + ".in");
ofstream g(file + ".out");

int n, ct = 0;
vector<int> v(20);
bitset<15> used_columns;

void write()
{
    ct++;
    if (ct == 1) {
        for (int i = 1; i <= n; i++)
            g << v[i] << sp;
        g << "\n";
    }
}

int chk(int ci) //current i
{
    if (used_columns[v[ci]] == 1)
        return 0;
    for (int i = 1; i < ci; i++)
        if (v[ci] == v[i])
            return 0;
        else if (abs(v[ci] - v[i]) == abs(ci - i))
            return 0;
    return 1;
}

void bkt(int i)
{
    for (int j = 1; j <= n; j++)
    {
        v[i] = j;
        if (chk(i))
        {
            used_columns[j] = 1;
            if (i == n)
                write();
            else
                bkt(i + 1);
            used_columns[j] = 0;
        }
    }
}

int main() {
    f >> n;
    bkt(1);
    g << ct;
    return 0;
}