Cod sursa(job #1820700)

Utilizator Coroian_DavidCoroian David Coroian_David Data 2 decembrie 2016 08:41:09
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <cstdio>

using namespace std;

FILE *f, *g;

bool ok = 1;

int n, k;

int cnt;

int stk[15];

int modul(int a)
{
    if(a < 0)
        return -a;

    return a;
}

void readFile()
{
    f = fopen("damesah.in", "r");

    fscanf(f, "%d", &n);

    fclose(f);
}

void printFile()
{
    g = fopen("damesah.out", "w");

    int i;

    for(i = 1; i <= n; i ++)
        fprintf(g, "%d ", stk[i]);

    fprintf(g, "\n");
}

bool valid()
{
    int i;

    for(i = 1; i < k; i ++)
    {
        if(stk[i] == stk[k] || modul(stk[i] - stk[k]) == modul(i - k))
            return false;
    }

    return true;
}

void solve()
{
    k = 1;

    while(k != 0)
    {
        if(stk[k] < n)
        {
            stk[k] ++;

            if(valid())
            {
                if(k == n)
                {
                    if(ok == 1)
                    {
                        ok = 0;

                        printFile();

                        cnt = 1;
                    }

                    else
                        cnt ++;
                }

                else
                {
                    k ++;

                    stk[k] = 0;
                }
            }
        }

        else
            k --;
    }
}

void printResult()
{
    fprintf(g, "%d\n", cnt);

    fclose(g);
}

int main()
{
    readFile();

    solve();

    printResult();

    return 0;
}