Cod sursa(job #1248980)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 26 octombrie 2014 12:20:24
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include    <iostream>
#include    <fstream>
#include    <cmath>

using namespace std;

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

int queen[15], N;
bool col[15], main_diag[30], sec_diag[30];
int answers;

void Backtrack(int l, int n)
{
    if (l == n)
    {
        if (answers < 1)
        {
            for(int c = 0; c < n; c++)
                fout << queen[c] + 1 << " ";
            fout << "\n";
        }
        answers += 1;
    }
    else
    {
        for (int c = 0; c < n; c++)
        {
            if (!col[c] && !main_diag[c - l + n - 1] && !sec_diag[l + c])
            {
                queen[l] = c;
                col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = true;
                Backtrack(l + 1, n);
                col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = false;
            }
        }
    }
}

void Read()
{
    fin >> N;
    Backtrack(0, N);
    fout << answers << "\n";
}

int main()
{
    Read();
    return 0;
}