Cod sursa(job #1692429)

Utilizator mimisorMimi Birau mimisor Data 20 aprilie 2016 20:32:10
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int st[14], n, sol=0;
void tip()
{
    int i;
    for (i=1; i<=n; i++)
        fout<<st[i]<<" ";
}
int valid (int k)
{
    int i;
    for (i=1; i<k; i++)
        if (abs(st[k]-st[i])==k-i||st[i]==st[k])
            return 0;
    return 1;
}
void back(int k)
{
    int i;
    for (i=1; i<=n; i++)
    {
        st[k]=i;
        if (valid(k))
        if (k==n)
        {
            sol++;
            if (sol==1)
             tip();
        }else
        back(k+1);
    }
}
int main()
{
    fin>>n;
    back(1);
    fout<<"\n"<<sol;
    fout.close();
    return 0;
}