Cod sursa(job #2823090)

Utilizator roberttrutaTruta Robert roberttruta Data 26 decembrie 2021 21:52:25
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>

using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");

int n, nr, poz[15], viz[15];

void vizit(int col)
{
    int j;
    for(j=1; j<=n; j++)
        viz[j] = 0;
    for(j=1; j<=col; j++)
    {
        int p = poz[j];
        viz[p] = 1;
        if(p-col+j-1 >= 1)
            viz[p-col+j-1] = 1;
        if(p+col-j+1 <= n)
            viz[p+col-j+1] = 1;
    }
}

void bck(int col)
{
    if(col > n)
    {
        nr++;
        if(nr == 1)
        {
            for(int i=1; i<=n; i++)
                g<<poz[i]<<' ';
            g<<'\n';
        }
        return;
    }
    for(int i=1; i<=n; i++)
        if(viz[i] == 0)
        {
            poz[col] = i;
            vizit(col);
            bck(col+1);
            poz[col] = 0;
            vizit(col-1);
        }
}

int main()
{
    f>>n;
    bck(1);
    g<<nr;

    return 0;
}