Pagini recente » Rating Szasz Tunde (tundesz) | Cod sursa (job #893758) | Cod sursa (job #450521) | Cod sursa (job #962213) | Cod sursa (job #3147148)
#include <iostream>
using namespace std;
const int NMAX = 14;
int n;
bool oriz[NMAX] , d1[2 * NMAX] , d2[2 * NMAX];
int ans[NMAX];
bool found = 0;
int total = 0;
void backt(int pos = 1)
{
if (pos == n + 1){
if (!found) {
for (int i = 1; i <= n; i++)
{
cout << ans[i] << ' ';
}
cout << '\n';
}
found = 1;
total++;
}
for (int i = 1; i <= n; i++)
{
// check oriz d1 d2
if (!oriz[i] && !d1[n + pos - i] && !d2[pos + i])
{
ans[pos] = i;
oriz[i] = 1;
d1[n + pos - i] = 1;
d2[pos + i] = 1;
backt(pos+1);
oriz[i] = 0;
d1[n + pos - i] = 0;
d2[pos + i] = 0;
}
}
}
int main ()
{
freopen("damesah.in" , "r" , stdin);
freopen("damesah.out" , "w" , stdout);
cin >> n;
backt();
cout << total << '\n';
}