Cod sursa(job #1541487)

Utilizator radiogard1999Dragoi Andrei radiogard1999 Data 4 decembrie 2015 09:46:01
Problema Problema Damelor Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>

using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");

int top,n;
int v[10],st[10],cnt;

void Afisare()
{
    if(cnt>0) return;
    for(int i=1;i<=n;i++)
        fout<<st[i]<<" ";
    fout<<"\n";
}

inline int Modul(int x)
{
    return x<0?-x:x;
}

bool Valid(int top,int i)
{
    int j;
    if(v[i]==1) return false;
    for(j=1;j<top;j++)
        if(Modul(st[j]-i)==Modul(j-top)) return false;
    return true;
}

void Back(int top)
{
    int i;
    if(top==n+1) {Afisare();cnt++;}
    else
        for(i=1;i<=n;i++)
            if(Valid(top,i))
        {
            v[i]=1;
            st[top]=i;
            Back(top+1);
            v[i]=0;
        }
}



int main()
{
    fin>>n;
    fin.close();
    Back(1);
    fout<<cnt<<"\n";
    fout.close();
    return 0;
}