Cod sursa(job #2795599)

Utilizator guzgandemunteIonescu Laura guzgandemunte Data 6 noiembrie 2021 17:39:17
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#define NMAX 13

using namespace std;

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

int n, nrSol;
int dama[NMAX], col[NMAX], mainDiag[2 * NMAX], secDiag[2 * NMAX];

void bck(int i)
{
    if (i == n) {
        if (nrSol == 0) for (int i = 0; i < n; ++i) fout << dama[i] + 1 << " ";
        ++nrSol;
    }
    else {
        for (int j = 0; j < n; ++j)
        if (!col[j] && !mainDiag[j - i + n - 1] && !secDiag[i + j]) {
                dama[i] = j;
                col[j] = mainDiag[j - i + n - 1] = secDiag[i + j] = true;
                bck(i + 1);
                col[j] = mainDiag[j - i + n - 1] = secDiag[i + j] = false;
        }
    }
}
int main()
{
    fin >> n;
    bck(0);
    fout << '\n' << nrSol;
}