Cod sursa(job #2471896)

Utilizator Antonio020712Potra Antonio Antonio020712 Data 11 octombrie 2019 18:09:37
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
// Sa se aranjeze n dame pe o tabla de sah n*n astfel incat sa nu se atace.

#include <fstream>
#include <cmath>

using namespace std;

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

int nrSolutii;
int regina[15];
bool col[15], diagP[30], diagS[30];

void back(int l, int n) {
    int j; 

    if (l == n) {
        if (nrSolutii < 1) {
            for (j = 0; j < n; j++)
                fout << regina[j] + 1 << ' ';
            fout << '\n';
        }
        nrSolutii++;
    } else {
        for (j = 0; j < n; j++) {
            if ((!col[j]) && (!diagP[j - l + n - 1]) && (!diagS[l + j])) {
                regina[l] = j;
                col[j] = diagP[j - l + n - 1] = diagS[l + j] = true;
                back(l + 1, n);
                col[j] = diagP[j - l + n - 1] = diagS[l + j] = false;
            } 
        }
    }
}

int main() {
    int n;

    fin >> n;

    back(0, n);
    fout << nrSolutii;

    fin.close();
    fout.close();

    return 0;
}