Cod sursa(job #2825519)

Utilizator walentines44Serban Valentin walentines44 Data 4 ianuarie 2022 20:02:55
Problema Problema Damelor Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

struct pos{
    int i, j;
} vect_pos[14];

int sol[14], n, nr_sol, ok;
ifstream fin("damesah.in");
ofstream fout("damesah.out");

bool valid(int k, int pos2){
    for(int x = 1; x < k; ++x){
        if(((vect_pos[x].i == k) || (vect_pos[x].j == pos2)) || (abs(k - vect_pos[x].i) == abs(pos2 - vect_pos[x].j))){
            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 i = k; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            if(valid(i, j)){
                sol[i] = j;
                vect_pos[k].i = i;
                vect_pos[k].j = j;
                bkt(k + 1);
            }
        }
    }
} 


int main(){

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

    return 0;
}