Pagini recente » Cod sursa (job #2723649) | Cod sursa (job #68197) | Cod sursa (job #676447) | Cod sursa (job #2729310) | Cod sursa (job #2722105)
#include <fstream>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
const int N = 14;
int n, queen[N], ans;
bool col[N], main_diag[N * 2], sec_diag[N * 2];
void bkt(int l, int n)
{
if(l == n)
{
if(ans < 1)
{
for(int c = 0; c < n; c++)
out << queen[c] + 1 << " ";
out << "\n";
}
ans++;
}
else
{
for(int c = 0; c < n; c++)
{
if(!col[c] && !main_diag[c - l + n - 1] && !sec_diag[l + c])
{
queen[l] = c;
col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = true;
bkt(l+1, n);
col[c] = main_diag[c - l + n - 1] = sec_diag[l + c] = false;
}
}
}
}
int main ()
{
in >> n;
bkt(0, n);
out << ans;
return 0;
}