Pagini recente » Cod sursa (job #42935) | Cod sursa (job #1673036) | Cod sursa (job #2123965) | Cod sursa (job #1605747) | Cod sursa (job #1107622)
#include <iostream>
#include <fstream>
using namespace std;
int sir[14],sir1[14],ok,n,total,diag1[27],diag2[27],linie[14];
void back (int poz)
{
int i,j,k,okp=1;
if (poz==n+1)
{
total++;
if (!ok) for (i=1; i<=n; i++) sir1[i]=sir[i];
ok=1;
}
else
{
for (i=1; i<=n; i++)
{
if (linie[i]) okp=0;
if (diag1[poz+i-1]) okp=0;
if (diag2[n+i-poz]) okp=0;
//cout<<poz<<" "<<i<<" "<<okp<<"\n";
if (okp)
{
sir[poz]=i;
linie[i]=1;
diag1[poz+i-1]=1;
diag2[n+i-poz]=1;
back(poz+1);
linie[i]=0;
diag1[poz+i-1]=0;
diag2[n+i-poz]=0;
}
else okp=1;
}
}
}
int main ()
{
int i;
FILE *f,*g;
f=fopen("damesah.in", "r");
g=fopen("damesah.out", "w");
fscanf(f, "%d", &n);
sir[0]=14;
back (1);
for (i=1; i<=n; i++) fprintf(g, "%d ", sir1[i]);
fprintf(g, "\n");
fprintf(g, "%d", total);
fclose(f); fclose(g);
return 0;
}