Cod sursa(job #2650769)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 20 septembrie 2020 00:42:19
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
long long written = 0;
int pozs[15];

int mask_col[30];
int mask_d1[30];
int mask_d2[30];

void bkt(int row)
{
    if(row == n)
    {
        if(written == 0)
        {
            for(int i = 0; i < n; i++)
                out << pozs[i]+1 << " ";
            out << endl;
        }
        written++;
    }

    for(int col = 0; col < n; col++)
    {
        if(mask_col[col] == 0 && mask_d1[row + col] == 0 && mask_d2[n - row + col] == 0)
        {
            mask_col[col] = 1;
            mask_d1[row + col] = 1;
            mask_d2[n - row + col] = 1;

            pozs[row] = col;

            bkt(row+1);

            mask_col[col] = 0;
            mask_d1[row + col] = 0;
            mask_d2[n - row + col] = 0;
        }
    }
}

int main()
{
    in >> n;

    bkt(0);

    out << written;
}