Cod sursa(job #1239125)

Utilizator cosmin79Carabet Cosmin Andrei cosmin79 Data 8 octombrie 2014 12:44:43
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>
#include <cstring>
#include <cmath>
#define NMAX 15
#define LMAX 35
using namespace std;
int n, P[NMAX], sol[NMAX], noSol;
bool mark[NMAX], markA[LMAX], markB[LMAX];

void back(int k)
{
    if (k == n + 1)
    {
        noSol++;
        if (noSol == 1)
            memcpy(sol, P, sizeof(P));
        return ;
    }
    
    for (int i = 1; i <= n; i++)
        if (!mark[i] && !markA[k - i + NMAX] && !markB[k + i])
        {
            P[k] = i;
            mark[i] = true; markA[k - i + NMAX] = true; markB[k + i] = true;
            back(k + 1);
            mark[i] = false; markA[k - i + NMAX] = false; markB[k + i] = false;
        }
}

int main()
{
    freopen("damesah.in", "r", stdin);
    freopen("damesah.out", "w", stdout);
    
    scanf("%d", &n);
    back(1);
    
    for (int i = 1; i <= n; i++)
        printf("%d ", sol[i]);
    printf("\n");
    printf("%d\n", noSol);
    return 0;
}