Pagini recente » Cod sursa (job #1540818) | Cod sursa (job #1032973) | Cod sursa (job #655838) | Cod sursa (job #845929) | Cod sursa (job #1922842)
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, st[15], nrsol=0, vizcol[15], vdiag1[30], vdiag2[30];
void solutie()
{
nrsol++;
if(nrsol==1)
for(int i=1; i<=n; i++)
fout<<st[i]<<' ';
}
void bt(int k)
{
for(int i=1; i<=n; i++)
{
st[k]=i;
if(!vizcol[st[k]] && !vdiag1[st[k]+k-1] && !vdiag2[st[k]-k-1+n])
{
vizcol[st[k]]=vdiag1[st[k]+k-1]=vdiag2[st[k]-k-1+n]=1;
if(k<n)
bt(k+1);
else solutie();
vizcol[st[k]]=vdiag1[st[k]+k-1]=vdiag2[st[k]-k-1+n]=0;
}
}
}
int main()
{
fin>>n; fin.close();
bt(1);
fout<<'\n';
fout<<nrsol<<'\n';
fout.close();
return 0;
}