Cod sursa(job #1357142)

Utilizator ArmandNMArmand Nicolicioiu ArmandNM Data 23 februarie 2015 19:43:36
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>

const int NMAX = 40;

using namespace std;

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

int N,contor;
int sol[NMAX];
bool linie[NMAX],coloana[NMAX],digI[NMAX],digII[NMAX];
bool afisez;

void check()
{
    if (afisez)
    {
        for (int i = 1; i <= N; ++i)
        {
            g << sol[i] << " ";
        }
        g << '\n';
        afisez = false;
    }

    contor++;
}

void backt(int k)
{
    if (k == N+1)
    {
        check();
        return;
    }

    int x = k;

    for (int y = 1; y <= N; ++y)
    {
        if (coloana[y])
            continue;
        if (digI[x-y+N])
            continue;
        if (digII[x+y])
            continue;

        coloana[y] = true;
        digI[x-y+N] = true;
        digII[x+y] = true;
        sol[k] = y;
        backt(k+1);
        coloana[y] = false;
        digI[x-y+N] = false;
        digII[x+y] = false;
    }
}

int main()
{
    f >> N;

    afisez = true;
    backt(1);
    g << contor;

    f.close();
    g.close();

    return 0;
}