Cod sursa(job #1829343)

Utilizator AndreidgDragomir Andrei Valentin Andreidg Data 14 decembrie 2016 20:21:08
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n,x[15],j;
long long nr=0;
bool gasit;
void afisare(){
    if(gasit==0)
    {
        gasit=1;
        nr=1;
        for(int i=1;i<=n;i++)
        {
            g<<x[i]<<" ";
        }
        g<<"\n";
    }
    else
    {
        nr++;
    }
}
bool corect(int i)
{
    for(j=1;j<i;j++)
    {
        if(x[i]==x[j])
        {
            return 0;
        }
        if(i-j==abs(x[j]-x[i]))
        {
            return 0;
        }
    }
    return 1;
}
void back(int i){
if(i<=n)
    {
    for(int j=1;j<=n;j++){
            x[i]=j;
            if(corect(i))//verific daca j este pe s1,s2,..s(i-1)
            {
                back(i+1);
            }
    }
    }
else
    afisare();
    return ;
}
int main()
{
    f>>n;
    back(1);
    g<<nr;
    return 0;
}