Cod sursa(job #2141604)

Utilizator StanCatalinStanCatalin StanCatalin Data 24 februarie 2018 14:50:06
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, st[17], v[14], sol[14], cnt=0, ok=0;

void Afisare()
{
    int i;
    cnt++;
    if (ok == 0)
      {
          for (i=1; i<=n; i++)
            sol[i] = st[i];
        ok = 1;
      }
}

void TheRealAfisare()
{
    int i;
    for (i = 1; i <= n; i++)
        out << sol[i] << " ";
    out << "\n";
    out << cnt << "\n";
}

int Validare(int i, int top)
{
    int j;
    if (v[i] == 1) return 0;
    for (j = 1; j < top; j++)
        if (abs(top-j) == abs(i-st[j]))
            return 0;
    return 1;
}

void Back(int top)
{
    int i;
    if (top == n+1)
        Afisare();
    else
        for (i=1; i<=n; i++)
        if (Validare(i, top))
    {
        v[i] = 1;
        st[top] = i;
        Back(top+1);
        v[i] = 0;
    }
}

int main()
{
    in >> n;
    Back(1);
    TheRealAfisare();
    return 0;
}