Cod sursa(job #3304686)

Utilizator radeuojArghira Radu Stefan radeuoj Data 26 iulie 2025 09:38:03
Problema Problema Damelor Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>
using namespace std;

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

constexpr int NMAX = 13;
int a[NMAX + 1];

int main() {
    int n;
    fin >> n;

    for (int i = 1; i <= n; i++) {
        a[i] = i;
    }

    long long ans = 0;
    bool first = true;
    do {
        bool ok = true;

        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (abs(i  - j) == abs(a[i] - a[j]))
                    ok = false;
            }
        }

        ans += ok;

        if (ok && first) {
            first = false;
            for (int i = 1; i <= n; i++) {
                fout << a[i] << ' ';
            }
            fout << '\n';
        }
    } while (next_permutation(a + 1, a + n + 1));

    fout << ans;

}