Cod sursa(job #2416856)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 28 aprilie 2019 13:01:15
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

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

int ind[15];
bool colblock[15], diagpr[30], diagsec[30];
int n, cnt;

void backtrack(int lin){
    if(lin == n + 1){
        if(cnt == 0){
            for(int i = 1; i <= n; ++i)
                fout << ind[i] << " ";
        }
        cnt++;
        return;
    }
    for(int col = 1; col <= n; ++col){
        if(!colblock[col] && !diagpr[15 - col + lin] && !diagsec[col + lin]){
            ind[lin] = col;
            colblock[col] = diagpr[15 - col + lin] = diagsec[col + lin] = 1;
            backtrack(lin + 1);
            colblock[col] = diagpr[15 - col + lin] = diagsec[col + lin] = 0;
        }
    }
}

int main()
{
    fin >> n;
    backtrack(1);
    fout << "\n" << cnt;
    return 0;
}