Pagini recente » Cod sursa (job #2549106) | Cod sursa (job #1670601) | Cod sursa (job #2176815) | Cod sursa (job #2256542) | Cod sursa (job #3273920)
// 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(vector<vector<char>> tabla, 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(tabla, megoldasok, lvl + 1);
kiralynok.pop_back();
}
}
}
}
int main()
{
in >> n;
vector<vector<char>> tabla(n, vector<char>(n));
int megoldasok = 0;
queens(tabla, megoldasok, 0);
out << "\n"
<< megoldasok;
return 0;
}