Cod sursa(job #2825543)

Utilizator walentines44Serban Valentin walentines44 Data 4 ianuarie 2022 20:22:27
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int sol[14], n, nr_sol, ok;

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

bool valid(int k, int j){
    for(int x = 1; x < k; ++x){
        if((sol[x] == j) || abs(k - x) == abs(j - sol[x])){
            return 0;
        }
    }

    return 1;
}

void bkt(int k){
    if(k == n + 1){
        if(!ok){
            for(int i = 1; i <= n; ++i){
                fout << sol[i] << " ";
            }
            fout << "\n";
            ok = 1;
        }
        nr_sol++;
        return;
    }

    for(int j = 1; j <= n; ++j){
        if(valid(k, j)){
            sol[k] = j;
            bkt(k + 1);
        }
    }
}


int main(){

    fin >> n;
    bkt(1);
    fout << nr_sol << "\n";

    return 0;
}