Pagini recente » Cod sursa (job #2193156) | Cod sursa (job #2244232) | Cod sursa (job #1635448) | Cod sursa (job #2682829) | Cod sursa (job #2028106)
#include <fstream>
using namespace std;
ifstream fi("damesah.in");
ofstream fo("damesah.out");
int n;
int dame[15];
int dameCount;
int solutions;
bool printed;
void printDame()
{
for(int i=1;i<=n;++i)
{
fo<<dame[i]<<' ';
}
fo<<'\n';
}
bool checkPeace(int i, int j)
{
if(!dameCount)
return true;
for(int k=1; k<=dameCount; ++k)
{
if(k == i || dame[k] == j || k - i == dame[k] - j || k - i == (-1)*(dame[k] - 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] = i;
placeDame(a+1);
--dameCount;
}
}
}
int main()
{
fi>>n;
placeDame(1);
fo<<solutions;
}