Cod sursa(job #2086878)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 12 decembrie 2017 17:07:34
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>
#include <cmath>
#define NMAX 14

using namespace std;

int n;
int st[NMAX], vf;

bool ok() {

    for (int i = 1; i < vf; i++)
        if ((st[i] == st[vf]) || (abs(st[i] - st[vf]) == vf - i))
            return false;

    return true;
}

int main()
{
    ifstream in("damesah.in");
    in >> n;
    in.close();

    vf = 1;
    st[vf] = 0;
    long long int nr_sol = 0;

    ofstream out("damesah.out");

    while (vf != 0) {

        st[vf]++;
        if (st[vf] > n)
            vf--;
        else {

            if (ok()) {

                if (vf == n) {

                    ++nr_sol;

                    if (nr_sol == 1) {

                        for (int i = 1; i <= n; i++)
                            out << st[i] << " ";
                        out << "\n";
                    }

                } else st[++vf] = 0;
            }
        }
    }
    out << nr_sol << "\n";
    out.close();
    return 0;
}