Cod sursa(job #3147148)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 24 august 2023 12:17:19
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>

using namespace std;
const int NMAX = 14;
int n; 
bool oriz[NMAX] , d1[2 * NMAX] , d2[2 * NMAX];
int ans[NMAX];
bool found = 0;
int total = 0;

void backt(int pos = 1)
{
    if (pos == n + 1){
        if (!found) {
            for (int i = 1; i <= n; i++)
            {
                cout << ans[i] << ' ';
            }
            cout << '\n';
        }
        found = 1;
        total++;
    } 
    for (int i = 1; i <= n; i++)
    {
        // check oriz d1 d2
        if (!oriz[i] && !d1[n + pos - i] && !d2[pos + i])
        {
            ans[pos] = i;
            oriz[i] = 1;
            d1[n + pos - i] = 1;
            d2[pos + i] = 1;
            backt(pos+1);
            oriz[i] = 0;
            d1[n + pos - i] = 0;
            d2[pos + i] = 0;
        }
    }
}

int main ()
{
    freopen("damesah.in" , "r" , stdin);
    freopen("damesah.out" , "w" , stdout);
    cin >> n;
    backt();
    cout << total << '\n';
}