Cod sursa(job #3150608)

Utilizator alex210046Bratu Alexandru alex210046 Data 17 septembrie 2023 17:35:02
Problema Problema Damelor Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, x[15], nrsol, ok;
bool d1[15], c[15], d2[30];

inline bool valid(int k, int v)
{
    if(!c[v] && !d1[n - k + v] && !d2[k + v])
        return 1;
    return 0;
}

void afis()
{
    nrsol++;
    if(ok) return;
    for(int i = 1; i <= n; i++)
        g << x[i] << ' ';
    g << '\n';
    ok = 1;
}

void backt(int k)
{
    if(k <= n)
        for(int v = 1; v <= n; v++)
        {
            x[k] = v;
            if(valid(k, v)) {
                c[v] = 1; d1[n - k + v] = 1; d2[k + v] = 1;
                backt(k + 1);
                c[v] = 0; d1[n - k + v] = 0; d2[k + v] = 0;
            }
        }
    else
        afis();
}


int main()
{
    f >> n;
    backt(1);
    g << nrsol;
    return 0;
}