Cod sursa(job #2610926)

Utilizator bmc213Mihai Cosmin bmc213 Data 5 mai 2020 21:38:25
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

int x[14], n, nr_sol;
bool ok;

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

void back(int k)
{
    for(int i = 1; i <= n; ++ i)
    {
        x[k] = i;
        if(valid(k))
        {
            if(k == n)
            {
                if(!ok)
                {
                    for(int j = 1; j <= n; ++ j)
                        g << x[j] << " ";
                    ok = 1;
                }
                nr_sol ++;
            }
            else
                back(k + 1);
        }
    }
}

int main()
{
    f >> n;
    back(1);
    g << "\n" << nr_sol;
    return 0;
}