Pagini recente » Istoria paginii runda/ojiji-becali/clasament | Cod sursa (job #371434) | Cod sursa (job #257205) | Cod sursa (job #1204070) | Cod sursa (job #1901031)
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
const int N=15;
int n, st[N], sol;
bool found;
bool valid(int k)
{
for(int i=1;i<k;++i)
{
if(i-st[i]==k-st[k])
return false;
if(i+st[i]==k+st[k])
return false;
if(st[i]==st[k])
return false;
}
return true;
}
void afis()
{
for(int i=1;i<=n;++i)
fout<<st[i]<<" ";
}
void bk(int k)
{
for(st[k]=1;st[k]<=n;++st[k])
{
if(valid(k))
{
if(k==n)
{
if(!found)afis(), found=true;
sol++;
}
bk(k+1);
}
}
}
int main()
{
fin>>n;
bk(1);
fout<<'\n'<<sol;
return 0;
}