Cod sursa(job #1257019)

Utilizator bujorcatalin14Bujor Catalin bujorcatalin14 Data 7 noiembrie 2014 09:24:28
Problema Problema Damelor Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");

int st[15],n;
int nr,nr2;
int a[15],lg;

void Afisare(int top)
{
    int i,j;

    for(i=1;i<=top;i++)
     {
         for(j=1;j<=top;j++)
          if(st[i]==j) //fout<<st[i]<<" ";
            {
                a[++lg]=st[i];

            }
     }



}
int Valid(int top)
{
    int i;
    for(i=1;i<=top-1;i++)
     if(st[top]==st[i] || abs(st[top]-st[i])==top-i) return 0;
     return 1;
}

void Back()
{
    int cand, top;
    top=1; st[top]=0;
    while(top>0)
    {
        cand=0;
        while(!cand && st[top]<n)
        {
            st[top]++;
            cand=Valid(top);
        }
        if(!cand )top--;
        else
        if(top==n) {Afisare(top);  nr++;}//exit(0);}
        else
        {
            top++;
            st[top]=0;
        }
    }
}

int main()
{   int i;
    fin>>n;
    Back();
for(i=1;i<=n;i++)
fout<<a[i]<<" ";
fout<<"\n";
    fout<<nr;
    return 0;
}