Pagini recente » Cod sursa (job #240573) | Cod sursa (job #885924) | Cod sursa (job #1896516) | Cod sursa (job #1752076) | Cod sursa (job #1657633)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int mat[15][15];
int n, nr;
void afiseaza()
{
int ct = 0;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
if (mat[i][j] != 0)
printf("%d ", ct+1);
else
ct++;
}
ct = 0;
}
}
bool bun(int x, int y)
{
if (mat[x][y]!=0)
return false;
for (int i=1; i<=n; i++)
{
if (mat[x][i] !=0)
return false;
if (mat[i][y] !=0)
return false;
}
int c1 = x, c2 = y;
while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
{
c1--;
c2++;
if (mat[c1][c2]!=0)
return false;
}
c1 = x, c2 = y;
while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
{
c1++;
c2--;
if (mat[c1][c2]!=0)
return false;
}
c1 = x, c2 = y;
while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
{
c1--;
c2--;
if (mat[c1][c2]!=0)
return false;
}
c1 = x, c2 = y;
while(c1>=1 && c2 >=1 && c1<=n && c2<=n)
{
c1++;
c2++;
if (mat[c1][c2]!=0)
return false;
}
return true;
}
void bt(int i=1, int k = 1)
{
if (k > n)
{
if (nr == 0)
afiseaza();
nr++;
return;
}
if (i > n)
return;
for (int j=1; j<=n; j++)
{
if (mat[i][j] == 0)
{
if (bun(i, j))
{
mat[i][j] = 1;
bt(i+1, k+1);
mat[i][j] = 0;
}
}
}
bt(i+1, k);
}
int main()
{
freopen("damesah.in", "r", stdin);
freopen("damesah.out", "w", stdout);
scanf("%d", &n);
bt();
printf("\n%d", nr);
return 0;
}