Cod sursa(job #1996745)

Utilizator JustGingaGinga Tudor-Adrian JustGinga Data 2 iulie 2017 15:29:26
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
using namespace std;
ifstream in ("damesah.in");
ofstream out ("damesah.out");
struct Dama { int L, C, dP, dS; } D[14];
int n, nr;

int cont (int d) {
    for (int i = 1; i < d; i++)
        if (D[i].L == D[d].L or D[i].C == D[d].C or D[i].dP == D[d].dP or D[i].dS == D[d].dS)
            return 0;
    return 1;
}

void back (int l) {
    for (int c = 1; c <= n; c++) {
        D[l].L = l; D[l].C = c; D[l].dP = n + c - l; D[l].dS = l + c - 1;
        if (cont(l))
            if (l == n) {
                nr++;
                for (int i = 1; nr == 1 and i <= n; i++)
                    out << D[i].C << " ";
            }
            else back(l + 1);
        else D[l].C = D[l].dP = D[l].dS = 0;
    }
}

int main() {
    in >> n; back(1);
    out << '\n' << nr << '\n';
    out.close(); return 0;
}