Cod sursa(job #3304687)

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

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

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

void progress(int x, int y, int dx, int dy, int n) {
    for (int i = x + dx, j = y + dy; i <= n && i >= 1 && j <= n && j >= 1; i += dx, j += dy) {
        grid[i][j] = 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++) {
            if (!grid[i][a[i]]) {
                progress(i, a[i], 1, 1, n);
                progress(i, a[i], 1, -1, n);
            } else {
                ok = false;
                break;
            }
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                grid[i][j] = 0;
            }
        }

        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;

}