Cod sursa(job #2691441)

Utilizator MattiaMattia Iojica Mattia Data 28 decembrie 2020 17:49:59
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("damesah.in");
ofstream g("damesah.out");

int n, v[14], sol;

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

void bkt(int k)
{
    if(k == n)
    {
        if(!sol)
            write();
        sol++;
    }
    else
        for(int i = 0; i < n; i++)
        {
            int ok = 1;
            for(int j = 0; j < k; j++)
                if(v[j] == i || abs(v[j] - i) == (k - j))
                    ok = 0;
                if(ok)
                {
                    v[k] = i;
                    bkt(k + 1);
                }
        }
}

int main()
{
    f >> n;

    bkt(0);
    g << '\n';
    g << sol;
    return 0;
}