Cod sursa(job #3273921)

Utilizator PatrikKev75Szucs Patrik - Kevin PatrikKev75 Data 4 februarie 2025 15:59:57
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
// https://www.infoarena.ro/problema/damesah
#include <fstream>
#include <vector>

using namespace std;

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

vector<pair<short, short>> kiralynok;
short n = 8;

void queens(int &megoldasok, short lvl)
{
    if (lvl == n)
    {
        megoldasok++;

        if (megoldasok == 1)
        {
            for (auto it = kiralynok.begin(); it < kiralynok.end(); it++)
            {
                out << it->second + 1 << " ";
            }
        }
    }
    else // lvl helyetesit egy for ciklust i-vel
    {

        for (short j = 0; j < n; j++) // hogyha 0 irok akkor vegtelenszer irja ki ugyanazt
        {
            bool ures = true;
            for (auto k = kiralynok.begin(); k < kiralynok.end(); k++)
            {
                if (lvl == k->first || j == k->second || abs(lvl - k->first) == abs(j - k->second)) // k -> first ugyanaz mint *(k.first)
                {
                    ures = false;
                    break;
                }
            }

            if (ures)
            {
                kiralynok.push_back({lvl, j});
                queens(megoldasok, lvl + 1);
                kiralynok.pop_back();
            }
        }
    }
}

int main()
{
    in >> n;

    int megoldasok = 0;

    queens(megoldasok, 0);

    out << "\n"
        << megoldasok;

    return 0;
}