Cod sursa(job #2682149)

Utilizator Tudor_PascaTudor Pasca Tudor_Pasca Data 7 decembrie 2020 21:43:22
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, ans, offset = 15;
int v[15];
bool fr[15], diagPr[35], diagSec[15];

void print()
{
    for(int i = 1; i <= n; i++)
        out << v[i] << ' ';
    out << '\n';
}

bool check(int index)
{
    //cout << index << '\n';

    if(fr[v[index]] == true || diagPr[index - v[index] + offset] == true || diagSec[index + v[index]] == true)
        return false;

    //cout << "true" << '\n';

    fr[v[index]] = true;
    diagPr[index - v[index] + offset] = true;
    diagSec[index + v[index]] = true;

    return true;
}

void sah(int index)
{
    for(v[index] = 1; v[index] <= n; v[index]++)
    {
        /*cout << "INDEX: " << index << '\n';
        cout << "VALUE: " << v[index] << '\n';
        cout << fr[v[index]] << ' ' << diagPr[index - v[index] + offset] << ' ' << diagSec[index + v[index]] << "\n\n";*/

        if(index == n + 1)
        {
            if(ans == 0)
                print();
            ans++;
            return;
        }
        else if(check(index))
        {
            sah(index + 1);
            fr[v[index]] = false;
            diagPr[index - v[index] + offset] = false;
            diagSec[index + v[index]] = false;
        }
    }
}

int main()
{
    in >> n;

    sah(1);

    out << ans << '\n';

    return  0;
}