Pagini recente » Cod sursa (job #1919826) | Cod sursa (job #2211416)
#include <bits/stdc++.h>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n,cnt,gasit,p[15],c[30],dp[30],ds[30];
void bkt(int);
int main()
{
f>>n;
bkt(1);
g<<cnt;
return 0;
}
void bkt(int i)
{
int j;
if(i==n+1)
{
if(!gasit)
{
gasit=1;
for(j=1; j<=n; j++)
g<<p[j]<<" ";
g<<"\n";
}
cnt++;
}
for(j=1; j<=n; j++)
if(c[j]==0&&dp[n+i-j]==0&&ds[i+j]==0)
{
c[j]=1;
dp[n+i-j]=1;
ds[i+j]=1;
p[i]=j;
bkt(i+1);
c[j]=0;
dp[n+i-j]=0;
ds[i+j]=0;
}
}