Cod sursa(job #2776894)

Utilizator Madalin_IonutFocsa Ionut-Madalin Madalin_Ionut Data 21 septembrie 2021 15:16:54
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

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

int st[14], n, nr_afis, ap_prin[26], ap_sec[26], ap[14];

void Afisare()
{
    nr_afis++;
    if (nr_afis <= 1)
    {
        for (int i = 1; i <= n; i++)
            fout << st[i] << " ";
        fout << "\n";
    }
}

int Valid(int top)
{
    if(ap[st[top]] == 1) return 0;
    if (st[top] >= top && ap_prin[n - st[top] + top] == 1) return 0;
    if (st[top] < top && ap_prin[n + top - st[top]] == 1) return 0;
    if (st[top] < n - top + 1 && ap_sec[st[top] + top - 1] == 1) return 0;
    if (st[top] >= n - top + 1 && ap_sec[top + st[top] - 1] == 1) return 0;
    if (st[top] >= top) ap_prin[n - st[top] + top] = 1;
    else ap_prin[n + top - st[top]] = 1;
    if (st[top] >= n - top + 1) ap_sec[top + st[top] - 1] = 1;
    else ap_sec[st[top] + top - 1] = 1;
    ap[st[top]] = 1;
    return 1;
}

void Remove(int top)
{
    if (st[top] >= top) ap_prin[n - st[top] + top] = 0;
    else ap_prin[n + top - st[top]] = 0;
    if (st[top] >= n - top + 1) ap_sec[top + st[top] - 1] = 0;
    else ap_sec[st[top] + top - 1] = 0;
    ap[st[top]] = 0;
}

void Back()
{
    int top, cand;
    top = 0;
    st[++top] = 0;
    while (top > 0)
    {
        cand = 0;
        while (cand == 0 && st[top] < n)
        {
            st[top]++;
            cand = Valid(top);
        }
        if (cand == 0)
        {
            top--;
            Remove(top);
        }
        else if (top == n)
        {
            Remove(top);
            Afisare();
        }
        else st[++top] = 0;
    }
}

int main()
{
    fin >> n;
    Back();
    fout << nr_afis << "\n";
    fin.close();
    fout.close();
}