Pagini recente » Cod sursa (job #3215605) | Cod sursa (job #479691) | Cod sursa (job #2594267) | Cod sursa (job #2609296) | Cod sursa (job #3164110)
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin("damesah.in");
ofstream cout("damesah.out");
int n,nr,f[15],sol[15];
bool gasit=0;
bool valid(int k){
for(int i=1;i<k;i++)
if(k-i==abs(sol[k]-sol[i]))
return 0;
return 1;
}
void backtracking(int k){
if(k==n+1)
{
if(gasit==0)
{
for(int i=1;i<=n;i++)
cout<<sol[i]<<" ";
cout<<'\n';
gasit=1;
}
nr++;
}
else
for(int i=1;i<=n;i++)
if(f[i]==0)
{
sol[k]=i;
f[i]=1;
if(valid(k))
backtracking(k+1);
f[i]=0;
}
}
int main()
{
cin>>n;
backtracking(1);
cout<<nr;
return 0;
}