Cod sursa(job #1657633)

Utilizator gabor.vlad13lil pump gabor.vlad13 Data 20 martie 2016 17:31:30
Problema Problema Damelor Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int mat[15][15];

int n, nr;

void afiseaza()
{
    int ct = 0;
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=n; j++)
        {
            if (mat[i][j] != 0)
                printf("%d ", ct+1);
            else
                ct++;
        }

        ct = 0;
    }
}

bool bun(int x, int y)
{
    if (mat[x][y]!=0)
        return false;

    for (int i=1; i<=n; i++)
    {
        if (mat[x][i] !=0)
            return false;
        if (mat[i][y] !=0)
            return false;
    }

    int c1 = x, c2 = y;
    while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
    {
        c1--;
        c2++;
        if (mat[c1][c2]!=0)
            return false;

    }

    c1 = x, c2 = y;

    while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
    {
        c1++;
        c2--;
        if (mat[c1][c2]!=0)
            return false;

    }

    c1 = x, c2 = y;

    while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
    {
        c1--;
        c2--;
        if (mat[c1][c2]!=0)
            return false;

    }

    c1 = x, c2 = y;

    while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
    {
        c1++;
        c2++;
        if (mat[c1][c2]!=0)
            return false;

    }

    return true;
}

void bt(int i=1, int k = 1)
{

    if (k > n)
    {
        if (nr == 0)
            afiseaza();
        nr++;
        return;
    }

    if (i > n)
        return;

    for (int j=1; j<=n; j++)
    {
        if (mat[i][j] == 0)
        {
            if (bun(i, j))
            {
                mat[i][j] = 1;
                bt(i+1, k+1);
                mat[i][j] = 0;
            }
        }
    }

    bt(i+1, k);

}
int main()
{
    freopen("damesah.in", "r", stdin);
    freopen("damesah.out", "w", stdout);
    scanf("%d", &n);
    bt();
    printf("\n%d", nr);
    return 0;
}