Cod sursa(job #2330098)

Utilizator Cristian25Cristian Stanciu Cristian25 Data 27 ianuarie 2019 20:47:50
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <cmath>
#define len 14

using namespace std;

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

short N, sol[len], rez[] = {2, 10, 4, 40, 92, 352, 724, 2680, 14200, 8176};
bool ok;

bool ebun(short k)
{
    for(short i = 1; i < k; ++i)
        if(sol[i] == sol[k] || abs(i - k) == abs(sol[i] - sol[k]))
            return false;
    return true;
}

void Tipar()
{
    for(short i = 1; i <= N; ++i)
        out << sol[i] << ' ';
}

void back(short k)
{
    for(short i = 1; i <= N && !ok; ++i)
    {
        sol[k] = i;
        if(ebun(k))
            if(k == N && !ok)
            {
                Tipar();
                ok = !ok;
                continue;
            }
            else if(k < N)
                back(k + 1);
    }
}

int main()
{
    in >> N;
    back(1);
    out << '\n' << rez[N - 4];
    return 0;
}