Cod sursa(job #2719097)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 9 martie 2021 16:19:40
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMax = 13;

int n, nr;
bool k;
int st[NMax + 5];
bool column[NMax + 5], principal[2 * NMax + 5], secondary[2 * NMax + 5];

void Read(){
    fin >> n;
}

void Print(){
    for (int i = 1; i <= n; i++)
        fout << st[i] << ' ';
    fout << '\n';
}

void Backtracking(int r){
    for (int c = 1; c <= n; c++){
        if (column[c]|| principal[r - c + n] || secondary[r + c])
            continue;
        st[r] = c;
        if (r == n){
            if (!k){
                Print();
                k = 1;
            }
            nr++;
        }
        else{
            column[c] = principal[r - c + n] = secondary[r + c] = 1;
            Backtracking(r + 1);
            column[c] = principal[r - c + n] = secondary[r + c] = 0;
        }
    }
}

int main(){
    Read();
    Backtracking(1);
    fout << nr << '\n';
    return 0;
}