Cod sursa(job #1593995)

Utilizator valentinoMoldovan Rares valentino Data 9 februarie 2016 08:11:07
Problema Problema Damelor Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
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)
{
    if(k == n+1)
    {
        if(valid())
        {
            nr++;
            if(!gasit)
                show(), gasit = 1;
        }
    }
    else
    {
        for(int i = 1;  i <= n; ++i)
        {
            if(!use[i])
            {
                x[k] = i;
                use[i] = 1;
                backk(k+1);
                use[i] = 0;
            }
        }
    }
}

int main()
{
    f >> n;
    backk(1);
    g << nr;
}