Cod sursa(job #3377137)

Utilizator Ianis1212Prentu Ianis Ianis1212 Data 21 noiembrie 2026 20:22:37
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int v[30], cnt = 0, n;

bool ok(int lin, int c)
{
    for (int i = 1; i < lin; i++)
    {
        if (v[i] == c || abs(lin - i) == abs(c - v[i]))
        {
            return false;
        }
    }
    return true;
}

void back(int lin)
{
    if (lin == n + 1)
    {
        cnt++;
        if (cnt == 1)
        {
            for (int i = 1; i <= n; i++)
            {
                fout << v[i] << " ";
            }
            fout << '\n';
        }
        return;
    }

    for (int c = 1; c <= n; c++)
    {
        if (ok(lin, c))
        {
            v[lin] = c;
            back(lin + 1);
        }
    }
}

int main()
{
    fin >> n;
    back(1);
    fout << cnt << '\n';
    return 0;
}