Cod sursa(job #3244359)

Utilizator FlaviuuuFlaviu Flaviuuu Data 24 septembrie 2024 16:36:19
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
ifstream cin("damesah.in");
ofstream cout("damesah.out");
#define ll long long
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
ll n;
vector<ll> a;
bool fr[15];
bool fr2[15][15];
ll ans = 0;
bool ok = 0;
void afisare()
{
    for(int i = 0; i < a.size(); i++)
        cout<<a[i]<<" ";
    cout<<'\n';
}
bool verificare(int i, int j)
{
    int ci = i, cj = j;
    while(i >= 0 && j >= 0)
    {
        i--; j--;
        if(fr2[i][j] == 1)
            return 0;
    }
    i = ci, j = cj;
    while(i <= n && j <= n)
    {
        i++; j++;
        if(fr2[i][j] == 1)
            return 0;
    }
    i = ci, j = cj;
    while(i <= n && j >= 0)
    {
        i++; j--;
        if(fr2[i][j] == 1)
            return 0;
    }
    i = ci, j = cj;
    while(i >= 0 && j <= n)
    {
        i--; j++;
        if(fr2[i][j] == 1)
            return 0;
    }
    return 1;
}
void bkt(int pas)
{
    if(pas == n)
        {if(ok == 0) afisare(), ok = 1; ans++; return;}
    for(int i = 1; i <= n; i++)
    {
        if(fr[i] == 0 && verificare(pas, i) == 1)
        {
            fr[i] = 1;
            fr2[pas][i] = 1;
            a.push_back(i);
            bkt(pas + 1);
            fr[i] = 0;
            fr2[pas][i] = 0;
            a.pop_back();
        }
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n;
    bkt(0);
    cout<<ans;
    return 0;
}