Cod sursa(job #3210640)

Utilizator alexandraiacobelAlexandra Iacob alexandraiacobel Data 6 martie 2024 21:33:02
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;

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

int n,mod[15],cnt=0; //prima solutie
bool used[15];

void afisare()
{
    for(int i=1; i<=n; i++)
    {
        fout<<mod[i]<<' ';
    }
    fout<<'\n';
}

bool ok(int l)
{
    for(int i=1; i<l; i++)
       if( mod[l] == mod[i] || abs(mod[i] - mod[l]) == abs(i-l))  ///YAHu
            return false;
       return true;

}
void back(int poz)
{
    if(poz>n)
    {
       if(!cnt)
            afisare();
        cnt++;
        return;
    }
    else
    {
         for(int i=1; i<=n; i++)
         {
             mod[poz]=i; ///DC TRB AICI SI NU in if-ul de jos???

             /*!  EXPLICATIE!

                Pai cum merge ok() daca mod[poz] nu exista, bA

             */
            if(!used[i] && ok(poz)) //acum vad daca trec mai departe
                {
                        used[i]=true;
                        back(poz+1);
                        used[i]=false;
                }
         }
    }

}

int main()
{
    fin>>n;

    back(1);

    fout<<cnt;

    return 0;
}