Cod sursa(job #2575303)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 6 martie 2020 12:48:08
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

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

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 15;

int ans[N], sol[N], n;
bool f[N];

bool check(int k)
{
    if(f[ans[k]] == true) return false;
    for(int i = 1; i < k; ++i)
        if(k - i == abs(ans[i] - ans[k])) return false;
    return true;
}

void solve(int k)
{
    for(int i = 1; i <= n; ++i) {
        ans[k] = i;
        if(check(k) == true) {
            f[i] = true;
            if(k == n) {
                if(sol[0] == 0) for(int j = 1; j <= n; ++j) sol[j] = ans[j];
                ++sol[0];
            }
            else solve(k + 1);
            f[i] = false;
        }
    }
}

int main()
{
    usain_bolt();

    fin >> n;
    solve(1);
    for(int i = 1; i <= n; ++i) fout << sol[i] << " ";
    fout << "\n" << sol[0];
    return 0;
}