Pagini recente » Cod sursa (job #1544505) | Cod sursa (job #404633) | Cod sursa (job #1913601) | Cod sursa (job #1094008) | Cod sursa (job #3273921)
// 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;
}