Cod sursa(job #1135213)

Utilizator belarusAlexeyAlex Craciun belarusAlexey Data 7 martie 2014 14:58:49
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n,k,st[100],p,contor=0;
int validare(int k)
{
    for(int i=1; i<k; i++)
        if(st[i]==st[k] || abs(st[k]-st[i])==k-i)
            return 0;
    return 1;
}

void tiparire()
{
    for(int i=1; i<=n; i++)
        fout<<st[i]<<" ";
        fout<<"\n";
}

void backtr(int k)
{
    for(int i=1; i<=n; i++)
    {
        st[k]=i;
        if(validare(k))
            if(k==n)
                {
                    if(contor==0) tiparire();
                    contor++;
                }
            else backtr(k+1);
    }
}

int main()
{
    fin>>n;
    backtr(1);
    fout<<contor;
    return 0;
}