Cod sursa(job #3244653)

Utilizator Yannis_TheodorBalan Yannis Theodor Yannis_Theodor Data 25 septembrie 2024 21:19:54
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>

using namespace std;
ifstream cin("damesah.in");
ofstream cout("damesah.out");
const int MAX_N = 14;
int q[MAX_N];
bool col[MAX_N], dig1[MAX_N * 2], dig2[MAX_N * 2];
int r;
int n;
void bkt(int l, int n )
{
    if ( l == n )
    {
        if ( r < 1 )
        {
            for ( int c = 0; c < n; c++ )
                cout<<q[c]+1<<" ";
            cout<<endl;
        }
        ++r;
    }
    else
    {
        for ( int c = 0; c < n; c++ )
        {
            if ( !col[c] && !dig1[c - l + n - 1] && !dig2[l + c] )
            {
                q[l] = c;

                col[c] = dig1[c - l + n - 1] = dig2[l + c] = true;
                bkt(l + 1, n );
                col[c] = dig1[c - l + n - 1] = dig2[l + c] = false;
            }
        }
    }
}
int main()
{
    cin>>n;
    bkt(0, n);
    cout<<r;
    return 0;
}