Cod sursa(job #2279137)

Utilizator DanutAldeaDanut Aldea DanutAldea Data 8 noiembrie 2018 22:43:02
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
using namespace std;

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

int n,cnt,v[20],f[20];

int check(int col, int pas){
    for(int i=1;i<pas;i++){
        if(v[i]-i==v[pas]-pas)
            return 0;
        if(v[i]+i==v[pas]+pas)
            return 0;
    }

    return 1;
}

void sheah(int pas){
    if(pas>n){
        cnt++;
        if(cnt==1){
            for(int i=1;i<=n;i++)
                fout<<v[i]<<" ";
            fout<<"\n";
        }

        return;
    }

    for(int i=1;i<=n;i++){
        if(f[i]==0){
            f[i]=1;
            v[pas]=i;
            if(check(i,pas))
                sheah(pas+1);
            f[i]=0;
        }
    }
}

int main(){
    fin>>n;
    sheah(1);
    fout<<cnt;

    return 0;
}