Pagini recente » Cod sursa (job #3132179) | Cod sursa (job #113408) | Cod sursa (job #373243) | Cod sursa (job #2910384) | Cod sursa (job #2342334)
#include <fstream>
#include <cmath>
using namespace std;
int st[100];
int n, s=0;
bool af=false;
ifstream f("damesah.in");
ofstream g("damesah.out");
bool succesor(int k)
{
if(st[k]<n)
{
st[k]++;
return true;
}
return false;
}
bool valid(int k)
{
for(int i=1; i<k; i++)
if(st[i]==st[k] || abs(st[k]-st[i])==k-i)
return false;
return true;
}
void back(int k)
{
if(k==n+1)
{
if(!af){
for(int i=1; i<=n; i++)
g<<st[i]<<" ";
g<<'\n';
af=true;
}
s++;
}
else
{
st[k]=0;
while(succesor(k))
if(valid(k))
back(k+1);
}
}
int main()
{
f>>n;
back(1);
g<<s;
return 0;
}