Cod sursa(job #2830124)

Utilizator divadddDavid Curca divaddd Data 9 ianuarie 2022 15:34:14
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#define MAX 20
using namespace std;
int n,st[MAX],nrsol,f[MAX];

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

bool verif(int pas){
    for(int i = 1; i < pas; i++){
        if(pas-i == abs(st[pas]-st[i])){
            return false;
        }
    }
    return true;
}

void gen(int pas){
    if(pas == n+1){
        nrsol++;
        if(nrsol == 1){
            for(int i = 1; i <= n; i++){
                fout << st[i] << " ";
            }
            fout << "\n";
        }
    }else{
        for(int i = 1; i <= n; i++){
            if(f[i] == 0){
                f[i] = 1;
                st[pas] = i;
                if(verif(pas)){
                    gen(pas+1);
                }
                f[i] = 0;
            }
        }
    }
}

int main()
{
    fin >> n;
    gen(1);
    fout << nrsol << "\n";
    return 0;
}