Pagini recente » Istoria paginii runda/testround01/clasament | Istoria paginii runda/hoata-pls2/clasament | Istoria paginii runda/grafuri/clasament | Istoria paginii runda/test_casian | Cod sursa (job #2028082)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fi("damesah.in");
ofstream fo("damesah.out");
int n;
pair<int,int> dame[15];
int dameCount;
int solutions;
bool printed;
void printDame()
{
for(int i=1;i<=n;++i)
{
fo<<dame[i].second<<' ';
}
fo<<'\n';
}
bool checkPeace(int i, int j)
{
if(!dameCount)
return true;
for(int k=1; k<=dameCount; ++k)
{
if(dame[k].first - i == dame[k].second - j || dame[k].first - i == (-1)*(dame[k].second - j) || dame[k].first == i || dame[k].second == j)
return false;
}
return true;
}
void placeDame(int a)
{
if(a == n+1 && n == dameCount)
{
if(!printed)
{
printDame();
printed = true;
}
++solutions;
}
for(int i=1; i<=n; ++i)
{
if(checkPeace(a,i))
{
++dameCount;
dame[dameCount].first = a;
dame[dameCount].second = i;
placeDame(a+1);
--dameCount;
}
}
}
int main()
{
fi>>n;
placeDame(1);
fo<<solutions;
}