Pagini recente » Cod sursa (job #1307090) | Cod sursa (job #2990560) | Cod sursa (job #1376631) | Cod sursa (job #2976924) | Cod sursa (job #2823090)
#include <fstream>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, nr, poz[15], viz[15];
void vizit(int col)
{
int j;
for(j=1; j<=n; j++)
viz[j] = 0;
for(j=1; j<=col; j++)
{
int p = poz[j];
viz[p] = 1;
if(p-col+j-1 >= 1)
viz[p-col+j-1] = 1;
if(p+col-j+1 <= n)
viz[p+col-j+1] = 1;
}
}
void bck(int col)
{
if(col > n)
{
nr++;
if(nr == 1)
{
for(int i=1; i<=n; i++)
g<<poz[i]<<' ';
g<<'\n';
}
return;
}
for(int i=1; i<=n; i++)
if(viz[i] == 0)
{
poz[col] = i;
vizit(col);
bck(col+1);
poz[col] = 0;
vizit(col-1);
}
}
int main()
{
f>>n;
bck(1);
g<<nr;
return 0;
}