Cod sursa(job #2432444)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 23 iunie 2019 17:50:56
Problema Problema Damelor Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 15;

int liber[DIM][DIM];

void pune(int x, int y, int r, int n)
{
    liber[x][y] += r;

    for(int i = x - 1, j = y - 1; i >= 1 && j >= 1; i--, j--)
        liber[i][j] += r;

    for(int i = x - 1, j = y + 1; i >= 1 && j <= n; i--, j++)
        liber[i][j] += r;

    for(int i = x + 1, j = y - 1; i <= n && j >= 1; i++, j--)
        liber[i][j] += r;

    for(int i = x + 1, j = y + 1; i <= n && j <= n; i++, j++)
        liber[i][j] += r;

    for(int i = x - 1; i >= 1; i--)
        liber[i][y] += r;

    for(int i = x + 1; i <= n; i++)
        liber[i][y] += r;

    for(int j = y - 1; j >= 1; j--)
        liber[x][j] += r;

    for(int j = y + 1; j <= n; j++)
        liber[x][j] += r;
}

int nr = 0;

int pos[DIM];

void bkt(int n, int lvl)
{
    if(lvl == n + 1)
    {
        nr++;

        if(nr == 1)
        {
            for(int i = 1; i <= n; i++)
                out << pos[i] << ' ';
            out << '\n';
        }

        return ;
    }

    for(int i = lvl; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(liber[i][j] == 0)
            {
                pune(i, j, 1, n);
                pos[i] = j;
                bkt(n, lvl + 1);
                pune(i, j, -1, n);
            }
}

int main()
{
    int n;
    in >> n;

    bkt(n, 1);

    out << nr << '\n';
}