Cod sursa(job #1593996)

Utilizator valentinoMoldovan Rares valentino Data 9 februarie 2016 08:18:38
Problema Problema Damelor Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

int x[100], use[100], n, nr, gasit;

int valid()
{
    for(int i = 2; i <= n; ++i)
    {
        for(int j = 1; j < i; ++j)
            if((x[i] - x[j] == i-j) || (x[i] - x[j] == -1*(i-j)))
            {
                return 0;
            }
    }
    return 1;
}
void show()
{
    for(int i = 1; i <= n; ++i)
        g << x[i] << ' ';
    g << '\n';
}

void backk(int k)
{
    do
    {
        if(valid())
        {
            nr++;
            if(!gasit)
                show(), gasit = 1;
        }
    }
    while(next_permutation(x+1,x+n+1));


}

int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        x[i] = i;
    backk(1);
    g << nr;
}