Pagini recente » Cod sursa (job #342155) | Cod sursa (job #511742) | Cod sursa (job #1449709) | Cod sursa (job #2877230) | Cod sursa (job #1541487)
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int top,n;
int v[10],st[10],cnt;
void Afisare()
{
if(cnt>0) return;
for(int i=1;i<=n;i++)
fout<<st[i]<<" ";
fout<<"\n";
}
inline int Modul(int x)
{
return x<0?-x:x;
}
bool Valid(int top,int i)
{
int j;
if(v[i]==1) return false;
for(j=1;j<top;j++)
if(Modul(st[j]-i)==Modul(j-top)) return false;
return true;
}
void Back(int top)
{
int i;
if(top==n+1) {Afisare();cnt++;}
else
for(i=1;i<=n;i++)
if(Valid(top,i))
{
v[i]=1;
st[top]=i;
Back(top+1);
v[i]=0;
}
}
int main()
{
fin>>n;
fin.close();
Back(1);
fout<<cnt<<"\n";
fout.close();
return 0;
}